basic issues with struct

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Apr 2008
Posts: 21
Reputation: MaestroRage is an unknown quantity at this point 
Solved Threads: 0
MaestroRage MaestroRage is offline Offline
Newbie Poster

basic issues with struct

 
0
  #1
Aug 31st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 251
Reputation: dwks has a spectacular aura about dwks has a spectacular aura about 
Solved Threads: 25
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: basic issues with struct

 
1
  #2
Aug 31st, 2009
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).
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 21
Reputation: MaestroRage is an unknown quantity at this point 
Solved Threads: 0
MaestroRage MaestroRage is offline Offline
Newbie Poster

Re: basic issues with struct

 
0
  #3
Aug 31st, 2009
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!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC