| | |
Error while declaring a structure
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2009
Posts: 29
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 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?
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 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:
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 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().
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; Nov 10th, 2009 at 12:48 pm.
•
•
Join Date: Dec 2007
Posts: 34
Reputation:
Solved Threads: 4
0
#5 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().
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: 29
Reputation:
Solved Threads: 0
0
#6 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().
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 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:
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: 29
Reputation:
Solved Threads: 0
0
#9 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:
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
Views: 316 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays bash binarysearch changingto char character cm copyanyfile copypdffile createprocess() database directory drawing dynamic execv feet fgets file floatingpointvalidation fork framework function functions getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list looping loopinsideloop. lowest matrix meter microsoft mqqueue mysql oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power process program programming pyramidusingturboccodes read recursion recv recvblocked reversing segmentationfault single socket socketprogramming spoonfeeding standard strchr string student suggestions system test testing threads unix urboc user whythiscodecausesegmentationfault win32api windowsapi






