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?

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.

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?

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.

I tried this and it compiled no problems

#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);
}

There are a million ways to do anything in C, and I'm glad someone got it to compile. Personally, rather than:

struct RECORD *head=(struct RECORD*)malloc(sizeof(struct RECORD));

I would have done something like:

struct RECORD *head=(RECORD *)malloc(sizeof(RECORD));

Actually, I would also check that malloc worked.

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().

#include <stdio.h>
int c = getchar();   /* wrong */

int main (void)
{
    printf("%c\n", c);
    return 0;
}

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().

#include <stdio.h>
int c = getchar();   /* wrong */

int main (void)
{
    printf("%c\n", c);
    return 0;
}

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.

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().

#include <stdio.h>
int c = getchar();   /* wrong */

int main (void)
{
    printf("%c\n", c);
    return 0;
}

Yeah. It compiled when I put the code for allocation in a method. Thanks.

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:

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.

***Deleted accidental duplicate 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:

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.