| | |
Error while declaring a structure
![]() |
•
•
Join Date: Oct 2009
Posts: 26
Reputation:
Solved Threads: 0
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?
I get an error on code lines 9-12.
C Syntax (Toggle Plain Text)
struct RECORD { char* username; char* password; char* type; struct RECORD *next; }; struct RECORD *head=(struct RECORD*)malloc(sizeof(struct RECORD)); struct RECORD *temp=(struct RECORD*)malloc(sizeof(struct RECORD)); struct RECORD *linktemp=(struct RECORD*)malloc(sizeof(struct RECORD)); struct RECORD *pos=(struct RECORD*)malloc(sizeof(struct RECORD));
I get an error on code lines 9-12.
0
#2 17 Days Ago
•
•
•
•
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?
C Syntax (Toggle Plain Text)
struct RECORD { char* username; char* password; char* type; struct RECORD *next; }; struct RECORD *head=(struct RECORD*)malloc(sizeof(struct RECORD)); struct RECORD *temp=(struct RECORD*)malloc(sizeof(struct RECORD)); struct RECORD *linktemp=(struct RECORD*)malloc(sizeof(struct RECORD)); struct RECORD *pos=(struct RECORD*)malloc(sizeof(struct RECORD));
I get an error on code lines 9-12.
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> struct RECORD { char* username; char* password; char* type; struct RECORD *next; }; int main(int argc, char**argv) { struct RECORD *head=(struct RECORD*)malloc(sizeof(struct RECORD)); struct RECORD *temp=(struct RECORD*)malloc(sizeof(struct RECORD)); struct RECORD *linktemp=(struct RECORD*)malloc(sizeof(struct RECORD)); struct RECORD *pos=(struct RECORD*)malloc(sizeof(struct RECORD)); exit(EXIT_SUCCESS); }
•
•
Join Date: Dec 2007
Posts: 33
Reputation:
Solved Threads: 4
0
#3 17 Days Ago
There are a million ways to do anything in C, and I'm glad someone got it to compile. Personally, rather than:
I would have done something like:
Actually, I would also check that malloc worked.
C Syntax (Toggle Plain Text)
struct RECORD *head=(struct RECORD*)malloc(sizeof(struct RECORD));
C Syntax (Toggle Plain Text)
struct RECORD *head=(RECORD *)malloc(sizeof(RECORD));
Actually, I would also check that malloc worked.
1
#4 17 Days Ago
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().
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().
c Syntax (Toggle Plain Text)
#include <stdio.h> int c = getchar(); /* wrong */ int main (void) { printf("%c\n", c); return 0; }
Last edited by Aia; 17 Days Ago at 12:48 pm.
•
•
Join Date: Dec 2007
Posts: 33
Reputation:
Solved Threads: 4
0
#5 17 Days Ago
•
•
•
•
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().
c Syntax (Toggle Plain Text)
#include <stdio.h> int c = getchar(); /* wrong */ int main (void) { printf("%c\n", c); return 0; }
•
•
Join Date: Oct 2009
Posts: 26
Reputation:
Solved Threads: 0
0
#6 17 Days Ago
•
•
•
•
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().
c Syntax (Toggle Plain Text)
#include <stdio.h> int c = getchar(); /* wrong */ int main (void) { printf("%c\n", c); return 0; }
•
•
Join Date: Aug 2009
Posts: 5
Reputation:
Solved Threads: 2
0
#7 16 Days Ago
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:
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.
You can allocate the structures at compile time if you want to. Outside of a function block:
C Syntax (Toggle Plain Text)
struct RECORD { char* username; char* password; char* type;} struct RECORD next; //Allocates memory for structure. struct Record * pnext=&next; //Provide a pointer reference to it. : : 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.
•
•
Join Date: Oct 2009
Posts: 26
Reputation:
Solved Threads: 0
0
#9 16 Days Ago
•
•
•
•
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:
C Syntax (Toggle Plain Text)
struct RECORD { char* username; char* password; char* type;} struct RECORD next; //Allocates memory for structure. struct Record * pnext=&next; //Provide a pointer reference to it. : : 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.
![]() |
Similar Threads
- Declaring a Structure in a Header File (C++)
- Help with Error about Struct and Variable Declaration (C++)
- i neeed ur help .. find error in data structure program (C++)
- Error while declaring Crystal Report Object (ASP.NET)
- Error While Declaring Crytal Report Object (VB.NET)
- How to delete a data structure in Builder 6.0? (C++)
Other Threads in the C Forum
- Previous Thread: Need help reversing words in a string and alphabetizing letters in the words.
- Next Thread: A whole bunch of Segmentation Faults
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators intmain() iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc program programming pyramidusingturboccodes read recursion recv recvblocked repetition research scanf scheduling segmentationfault send shape socketprograming socketprogramming stack standard strchr string suggestions systemcall test unix urboc user variable voidmain() wab win32api windows.h






