944,035 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 584
  • C RSS
Nov 10th, 2009
0

Error while declaring a structure

Expand 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.
Reputation Points: 7
Solved Threads: 0
Light Poster
shakunni is offline Offline
29 posts
since Oct 2009
Nov 10th, 2009
0
Re: Error while declaring a structure
Click to Expand / Collapse  Quote originally posted by shakunni ...
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. }
Reputation Points: 499
Solved Threads: 367
Postaholic
gerard4143 is online now Online
2,197 posts
since Jan 2008
Nov 10th, 2009
0
Re: Error while declaring a structure
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.
Reputation Points: 12
Solved Threads: 5
Light Poster
dan63043 is offline Offline
36 posts
since Dec 2007
Nov 10th, 2009
1
Re: Error while declaring a structure
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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Nov 10th, 2009
0
Re: Error while declaring a structure
Click to Expand / Collapse  Quote originally posted by Aia ...
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.
Reputation Points: 12
Solved Threads: 5
Light Poster
dan63043 is offline Offline
36 posts
since Dec 2007
Nov 10th, 2009
0
Re: Error while declaring a structure
Click to Expand / Collapse  Quote originally posted by Aia ...
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.
Reputation Points: 7
Solved Threads: 0
Light Poster
shakunni is offline Offline
29 posts
since Oct 2009
Nov 11th, 2009
0
Re: Error while declaring a structure
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.
Reputation Points: 10
Solved Threads: 5
Newbie Poster
sbesch is offline Offline
20 posts
since Aug 2009
Nov 11th, 2009
0
Re: Error while declaring a structure
***Deleted accidental duplicate post***
Last edited by sbesch; Nov 11th, 2009 at 11:54 am. Reason: Accidantal duplication
Reputation Points: 10
Solved Threads: 5
Newbie Poster
sbesch is offline Offline
20 posts
since Aug 2009
Nov 11th, 2009
0
Re: Error while declaring a structure
Click to Expand / Collapse  Quote originally posted by sbesch ...
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.
Reputation Points: 7
Solved Threads: 0
Light Poster
shakunni is offline Offline
29 posts
since Oct 2009

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:





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


Follow us on Twitter


© 2011 DaniWeb® LLC