943,512 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 363
  • C RSS
Aug 31st, 2009
0

basic issues with struct

Expand Post »
I'm having some trouble using struct which I can't seem to figure out. it seems to be following all the examples perfectly, i've tried to break it down to it's simplest block and nothing. Can somebody please tell me why this won't compile?

  1. #include <stdio.h>
  2.  
  3. typedef struct a{
  4. char a[25][12]={"a", "b", "c"};
  5. int b[12]={10, 5,1};
  6. };
  7.  
  8. struct test{
  9. char a[25][12]={"a", "b", "c", "d"};
  10. int b[12]={2, 1,1,3};
  11. int c[12]={1,1,1,2};
  12. };
  13.  
  14. int main (){
  15.  
  16.  
  17. return 0;
  18. }

I get this error
error C2143: syntax error : missing ';' before '='

but this error doesn't make much sense to me.

TIA.
Last edited by MaestroRage; Aug 31st, 2009 at 6:30 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
MaestroRage is offline Offline
21 posts
since Apr 2008
Aug 31st, 2009
1

Re: basic issues with struct

You can't assign anything inside a structure declaration. A structure declaration just defines a type. It's like a blueprint; you're telling the compiler, "if I ask you to create a Something, this is what I want it to look like." An example of a structure declaration would be
  1. struct Something {
  2. int x, y;
  3. double zoom;
  4. };
When you declare an instance of a structure, you're actually using memory. Here I create a variable of type Something:
  1. struct Something variable;
Since this is a variable, it can be initialized as you were doing above.
  1. struct Something variable = {1, 2, 5.0};
Of course, you can always do it manually too.
  1. struct Something variable;
  2. variable.x = 1;
  3. variable.y = 2;
  4. variable.zoom = 5.0;
Just think of a structure as a new type, like int or char -- not a built-in type, but rather a type you've defined in terms of built-in types (or other structures).
Reputation Points: 185
Solved Threads: 28
Posting Whiz in Training
dwks is offline Offline
269 posts
since Nov 2005
Aug 31st, 2009
0

Re: basic issues with struct

aaah!

I knew it had to be something simple like that. I'm having a terrible off day I can't believe I didn't see that.

Thanks a lot for the quick and very informative post!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
MaestroRage is offline Offline
21 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Conversion in C Language
Next Thread in C Forum Timeline: comparing string in array





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC