Error while declaring a structure

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

Join Date: Oct 2009
Posts: 29
Reputation: shakunni is an unknown quantity at this point 
Solved Threads: 0
shakunni shakunni is offline Offline
Light Poster

Error while declaring a structure

 
0
  #1
Nov 10th, 2009
As part of my program, I have defined and initialized a structure. But when I compile it, gcc gives me the following error on all the initialization " initializer element is not constant". Anyone know why this is the case?

  1. struct RECORD
  2. {
  3. char* username;
  4. char* password;
  5. char* type;
  6. struct RECORD *next;
  7. };
  8.  
  9. struct RECORD *head=(struct RECORD*)malloc(sizeof(struct RECORD));
  10. struct RECORD *temp=(struct RECORD*)malloc(sizeof(struct RECORD));
  11. struct RECORD *linktemp=(struct RECORD*)malloc(sizeof(struct RECORD));
  12. struct RECORD *pos=(struct RECORD*)malloc(sizeof(struct RECORD));

I get an error on code lines 9-12.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 431
Reputation: gerard4143 is on a distinguished road 
Solved Threads: 52
gerard4143's Avatar
gerard4143 gerard4143 is offline Offline
Posting Pro in Training
 
0
  #2
Nov 10th, 2009
Originally Posted by shakunni View Post
As part of my program, I have defined and initialized a structure. But when I compile it, gcc gives me the following error on all the initialization " initializer element is not constant". Anyone know why this is the case?

  1. struct RECORD
  2. {
  3. char* username;
  4. char* password;
  5. char* type;
  6. struct RECORD *next;
  7. };
  8.  
  9. struct RECORD *head=(struct RECORD*)malloc(sizeof(struct RECORD));
  10. struct RECORD *temp=(struct RECORD*)malloc(sizeof(struct RECORD));
  11. struct RECORD *linktemp=(struct RECORD*)malloc(sizeof(struct RECORD));
  12. struct RECORD *pos=(struct RECORD*)malloc(sizeof(struct RECORD));

I get an error on code lines 9-12.
I tried this and it compiled no problems

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct RECORD
  5. {
  6. char* username;
  7. char* password;
  8. char* type;
  9. struct RECORD *next;
  10. };
  11.  
  12.  
  13. int main(int argc, char**argv)
  14. {
  15. struct RECORD *head=(struct RECORD*)malloc(sizeof(struct RECORD));
  16. struct RECORD *temp=(struct RECORD*)malloc(sizeof(struct RECORD));
  17. struct RECORD *linktemp=(struct RECORD*)malloc(sizeof(struct RECORD));
  18. struct RECORD *pos=(struct RECORD*)malloc(sizeof(struct RECORD));
  19.  
  20.  
  21. exit(EXIT_SUCCESS);
  22. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 34
Reputation: dan63043 is an unknown quantity at this point 
Solved Threads: 4
dan63043 dan63043 is offline Offline
Light Poster
 
0
  #3
Nov 10th, 2009
There are a million ways to do anything in C, and I'm glad someone got it to compile. Personally, rather than:
  1. struct RECORD *head=(struct RECORD*)malloc(sizeof(struct RECORD));
I would have done something like:
  1. struct RECORD *head=(RECORD *)malloc(sizeof(RECORD));

Actually, I would also check that malloc worked.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,048
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 179
Aia's Avatar
Aia Aia is offline Offline
Postaholic
 
1
  #4
Nov 10th, 2009
shakunni >Anyone know why this is the case?
A function must be called from another function block which could be the main function or one with a lineage to any main function.
Meaning that main() must have been called first at some point.
malloc() is a function and you are trying to execute it before main().

  1. #include <stdio.h>
  2. int c = getchar(); /* wrong */
  3.  
  4. int main (void)
  5. {
  6. printf("%c\n", c);
  7. return 0;
  8. }
Last edited by Aia; Nov 10th, 2009 at 12:48 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 34
Reputation: dan63043 is an unknown quantity at this point 
Solved Threads: 4
dan63043 dan63043 is offline Offline
Light Poster
 
0
  #5
Nov 10th, 2009
Originally Posted by Aia View Post
shakunni >Anyone know why this is the case?
A function must be called from another function block which could be the main function or one with a lineage to any main function.
Meaning that main() must have been called first at some point.
malloc() is a function and you are trying to execute it before main().

  1. #include <stdio.h>
  2. int c = getchar(); /* wrong */
  3.  
  4. int main (void)
  5. {
  6. printf("%c\n", c);
  7. return 0;
  8. }
Aha! We (other posters) made assumptions about the code, because we had only a snippet. Looking back at the error received by the original poster, I'd say Aia has hit the nail on the head.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 29
Reputation: shakunni is an unknown quantity at this point 
Solved Threads: 0
shakunni shakunni is offline Offline
Light Poster
 
0
  #6
Nov 10th, 2009
Originally Posted by Aia View Post
shakunni >Anyone know why this is the case?
A function must be called from another function block which could be the main function or one with a lineage to any main function.
Meaning that main() must have been called first at some point.
malloc() is a function and you are trying to execute it before main().

  1. #include <stdio.h>
  2. int c = getchar(); /* wrong */
  3.  
  4. int main (void)
  5. {
  6. printf("%c\n", c);
  7. return 0;
  8. }
Yeah. It compiled when I put the code for allocation in a method. Thanks.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 5
Reputation: sbesch is an unknown quantity at this point 
Solved Threads: 2
sbesch sbesch is offline Offline
Newbie Poster
 
0
  #7
Nov 11th, 2009
I think that it might be useful to generalize this a bit more. The problem is that you didn't distinguish between what happens (or can happen) at compile time from what happens (or can happen) at run time - this is not always a simple thing to do. Nevertheless, since malloc is a function, it can't get called till the program runs, and as Aia pointed out, it can't get called if it's outside a function block.

You can allocate the structures at compile time if you want to. Outside of a function block:
  1. struct RECORD {
  2. char* username;
  3. char* password;
  4. char* type;}
  5.  
  6. struct RECORD next; //Allocates memory for structure.
  7. struct Record * pnext=&next; //Provide a pointer reference to it.
  8. :
  9. : etc.

You still need to allocate the char* items at runtime, but space for the structures and the pointer to the structures will be done for you by the compiler. I would guess that this is what you actually want. Use malloc when you have an indeterminate (or perhaps very large) number of structures that must be generated at run time - that's what its for: to generate blocks of memory allocation that would be either inefficient or undesirable to include in the program's executable file. Use the compiler to allocate when you have only a few, definite structures.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 5
Reputation: sbesch is an unknown quantity at this point 
Solved Threads: 2
sbesch sbesch is offline Offline
Newbie Poster
 
0
  #8
Nov 11th, 2009
***Deleted accidental duplicate post***
Last edited by sbesch; Nov 11th, 2009 at 11:54 am. Reason: Accidantal duplication
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 29
Reputation: shakunni is an unknown quantity at this point 
Solved Threads: 0
shakunni shakunni is offline Offline
Light Poster
 
0
  #9
Nov 11th, 2009
Originally Posted by sbesch View Post
I think that it might be useful to generalize this a bit more. The problem is that you didn't distinguish between what happens (or can happen) at compile time from what happens (or can happen) at run time - this is not always a simple thing to do. Nevertheless, since malloc is a function, it can't get called till the program runs, and as Aia pointed out, it can't get called if it's outside a function block.

You can allocate the structures at compile time if you want to. Outside of a function block:
  1. struct RECORD {
  2. char* username;
  3. char* password;
  4. char* type;}
  5.  
  6. struct RECORD next; //Allocates memory for structure.
  7. struct Record * pnext=&next; //Provide a pointer reference to it.
  8. :
  9. : etc.

You still need to allocate the char* items at runtime, but space for the structures and the pointer to the structures will be done for you by the compiler. I would guess that this is what you actually want. Use malloc when you have an indeterminate (or perhaps very large) number of structures that must be generated at run time - that's what its for: to generate blocks of memory allocation that would be either inefficient or undesirable to include in the program's executable file. Use the compiler to allocate when you have only a few, definite structures.
That is what I needed to do actually. I had to read an indeterminate number of lines from a text file and store the data on each line in one node of a linked list and perform data management operations on the linked list.
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 316 | Replies: 8
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC