Archived

This topic is now archived and is closed to further replies.

Real silly question about structs....

This topic is 5279 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I always used to declare structs as: typedef struct products { char name [30]; float price; }products; foo bar; //init a struct variable like so. but now I found in a book of mine they declared them like: struct products { char name [30]; float price; };//no struct object name products apple; //same init. But hen I try the second one from the book in Visual Studio 6, it gives me an absolute crapload of errors. What''s the exact difference between the two ways, and why doesn''t the second aproach work in visual studio? (I tried with and without typedef in both examples)

Share this post


Link to post
Share on other sites
What are the errors? I looked at the code, didn''t see anything wrong with it... So I copied and pasted that straight into VC++ 6.0 and there was no problem. Are you sure it''s not elsewhere, like how you define objects of that structure?

-Arek the Absolute

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Bad Maniac
I always used to declare structs as:
typedef struct products
{
char name [30];
float price;
}products;

foo bar; //init a struct variable like so.

but now I found in a book of mine they declared them like:
struct products
{
char name [30];
float price;
};//no struct object name

products apple; //same init.

But hen I try the second one from the book in Visual Studio 6, it gives me an absolute crapload of errors.
What''s the exact difference between the two ways, and why doesn''t the second aproach work in visual studio?

(I tried with and without typedef in both examples)


I too was able to declare the struct both ways without trouble.

Share this post


Link to post
Share on other sites
Warning -- I haven''t verified any of this.

Your top version was originally the C way of declaring a struct. I don''t know if there is a formal preference at this time. People should know both and shouldn''t be tripped up by them, so go with whatever you want as long as you are consistent.

I tend to use the struct name {}; format as it closer to the class declaration syntax.

Share this post


Link to post
Share on other sites

#include <stdio.h>

typedef struct TEST_STRUCT
{
int x;
int y;
int z;
};

void Set_Struct (TEST_STRUCT *bar);

void main (void)
{

TEST_STRUCT foo;
Set_Struct (&foo);
printf ("The Struct Contains: X=%i Y=%i Z=%i\n", foo.x, foo.y, foo.z);
}

void Set_Struct (TEST_STRUCT * bar)
{
bar->x = 7;
bar->y = 13;
bar->z = 69;
}

And the errors:
--------------------Configuration: struct pointer - Win32 Debug--------------------
Compiling...
Structptr.c
C:\Program Files\Microsoft Visual Studio\MyProjects\struct pointer\Structptr.c(10) : error C2143: syntax error : missing '')'' before ''*''
C:\Program Files\Microsoft Visual Studio\MyProjects\struct pointer\Structptr.c(10) : error C2143: syntax error : missing ''{'' before ''*''
C:\Program Files\Microsoft Visual Studio\MyProjects\struct pointer\Structptr.c(10) : error C2059: syntax error : '')''
C:\Program Files\Microsoft Visual Studio\MyProjects\struct pointer\Structptr.c(15) : error C2065: ''TEST_STRUCT'' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\struct pointer\Structptr.c(15) : error C2146: syntax error : missing '';'' before identifier ''foo''
C:\Program Files\Microsoft Visual Studio\MyProjects\struct pointer\Structptr.c(15) : error C2065: ''foo'' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\struct pointer\Structptr.c(16) : warning C4013: ''Set_Struct'' undefined; assuming extern returning int
C:\Program Files\Microsoft Visual Studio\MyProjects\struct pointer\Structptr.c(17) : error C2224: left of ''.x'' must have struct/union type
C:\Program Files\Microsoft Visual Studio\MyProjects\struct pointer\Structptr.c(17) : error C2224: left of ''.y'' must have struct/union type
C:\Program Files\Microsoft Visual Studio\MyProjects\struct pointer\Structptr.c(17) : error C2224: left of ''.z'' must have struct/union type
C:\Program Files\Microsoft Visual Studio\MyProjects\struct pointer\Structptr.c(20) : error C2143: syntax error : missing '')'' before ''*''
C:\Program Files\Microsoft Visual Studio\MyProjects\struct pointer\Structptr.c(20) : error C2143: syntax error : missing ''{'' before ''*''
C:\Program Files\Microsoft Visual Studio\MyProjects\struct pointer\Structptr.c(20) : error C2059: syntax error : '')''
C:\Program Files\Microsoft Visual Studio\MyProjects\struct pointer\Structptr.c(21) : error C2054: expected ''('' to follow ''bar''
Error executing cl.exe.

struct pointer.exe - 13 error(s), 1 warning(s)

see what I mean, god knows why =)

Share this post


Link to post
Share on other sites
Works just fine in VC++ 6.0 for me.
but i think you need to declare your struct like this ...

#include <stdio.h>
typedef struct
{
int x;
int y;
int z;
}TEST_STRUCT;

void Set_Struct (TEST_STRUCT *bar);
void main (void)
{ TEST_STRUCT foo;
Set_Struct (&foo);
printf ("The Struct Contains: X=%i Y=%i Z=%i\n", foo.x, foo.y, foo.z);
}void Set_Struct (TEST_STRUCT * bar)
{
bar->x = 7;
bar->y = 13;
bar->z = 69;
}


Share this post


Link to post
Share on other sites
The "typedef struct..." style is optional for C code. The non-typedef''d version is preferable for C++. The reason is simple: in C, you had to declare a struct with the word struct. E.g.:

struct Whatever
{
/* stuff goes here */
};

/* now declare some variables */
struct Whatever my_struct;
struct Whatever another_one;


Notice how you have to say "struct Whatever" instead of "Whatever" there? Well, people are lazy. Hence, they typedef the structs. There''s nothing special about the typedef -- it works in the same manner as any other typedef. In other words, you''re saying that "struct Whatever" can be simply referred to as "Whatever_you_called_it" (assuming you put a name at the end after the struct).

You don''t need the typedef in C++ since a struct is just like a class but with public default member visibility and inheritance. Hence, don''t bother with the typedef in C++.

Also: if you put code in a .c file rather than a .cpp file then VC++ will compile it as C rather than C++. If you omit the typedef when using C code (with a .c extension) then your compiler will complain -- you''ve not been saying "struct Whatever" but rather "Whatever" and it won''t know what''s going on.

Share this post


Link to post
Share on other sites
Well I'm using C, and C file extension, so in other words I need to
typedef struct foo
{...}
foo;

to get it to work, it's not a big deal, I just thought it was funny how many errors it could produce =)

Thanks a lot for your help guys, that's one more thing I know thanks to gamedev.
//MArtin

[edited by - Bad Maniac on July 6, 2003 7:09:14 AM]

Share this post


Link to post
Share on other sites