Confused about typedef for structures

I was informed that if I want to return a struct from a function I should typedef it. In regards to typedef'ing, which is the proper syntax for it. I tried reading up on it, but ended up more confused. My goal, create a struct, fill it with goodies and return it from a function.
This:
1
2
3
4
5
typedef struct {	
	int passedChannels;
	int passedSamples;	
	short **passedBuffers;
}BUFFER;


Or this:
1
2
3
4
5
typedef struct BUFFER{	
	int passedChannels;
	int passedSamples;	
	short **passedBuffers;
};


Or finally this:
1
2
3
4
5
typedef struct BUFFER{	
	int passedChannels;
	int passedSamples;	
	short **passedBuffers;
}buffer;


Or are all of these wrong?

Thanks
Chris



I was informed that if I want to return a struct from a function I should typedef it.

You should not.


1
2
3
4
5
6
7
8
9
10
11
struct buffer{	
	int passedChannels;
	int passedSamples;	
	short **passedBuffers;
};

buffer someFunction()
{
  buffer aNiceNewObject; // make a buffer object
  return aNiceNewObject; // return it
}
@Moschops:
Thank you very much.
the first 2 and the one that Moschops shower are completly the same, while the third makes 2 indentical structures, BUFFER and buffer.
So, yes, all of them are wrong, becouse typedefing structures is stupid :D
Last edited on
typdefing a struct is common in C.

1
2
3
4
5
6
struct S {};
S si;   //not ok in C, ok in C++
struct S si;  //required in C

typedef struct {}TS;
TS tsi;  //ok in C and C++ 

Last edited on
The first and the third declarations of the structure are correct. The second typedef is not correct because it does not introduce a new type name.
Topic archived. No new replies allowed.