| | |
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 21 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: 34
Reputation:
Solved Threads: 4
0
#3 21 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 21 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; 21 Days Ago at 12:48 pm.
•
•
Join Date: Dec 2007
Posts: 34
Reputation:
Solved Threads: 4
0
#5 21 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 21 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 20 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 20 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 |
#include * ansi array arrays asterisks bash binarysearch calculate centimeter changingto char character convert copyanyfile copyimagefile creafecopyofanytypeoffileinc createprocess() database dynamic execv fgets file floatingpointvalidation fork function getlogicaldrivestrin givemetehcodez grade gtkwinlinux histogram ide inches include infiniteloop initialization input interest intmain() iso kernel keyboard kilometer km license linked linkedlist linux list looping lowest matrix meter microsoft number oddnumber open opendocumentformat openwebfoundation owf pdf pointer pointers posix power probleminc process program programming pyramidusingturboccodes radix read recursion recv recvblocked research reversing scheduling segmentationfault send sequential single socket socketprogramming standard strchr string suggestions systemcall test threads turboc unix urboc user variable wab whythiscodecausesegmentationfault win32api windowsapi






