USING DATA STRUCTURES/MALLOC in CW 5.7.0

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

USING DATA STRUCTURES/MALLOC in CW 5.7.0

3,256 Views
timmy10ar
Contributor I
I am trying to create a simple structure with a pointer to it and I can't even get it to compile. I am assuming that this sort of coding is possible...what I would eventually like to do is have a linked list of items that I hard code at the start of the program and reference throughout. Here is what i have:

struct Item_Struct {
char Text[30];
char Price[6];
char BMP_NUM[3];
char BUT_NUM[3];
struct Item_Struct *Next;
};

struct Item_Struct *Head = NULL;

Head = malloc(sizeof(struct Item_Struct));
Head->Text = "Hamburger";
Head->Price = "$5.99";
Head->BMP_NUM = "45";
Head->BUT_NUM = "45";
Head->Next = NULL;
Labels (1)
0 Kudos
5 Replies

413 Views
pittbull
Contributor III
Hi Timmy,

timmy10ar wrote:

Head = malloc(sizeof(struct Item_Struct));
Head->Text = "Hamburger";
Head->Price = "$5.99";
Head->BMP_NUM = "45";
Head->BUT_NUM = "45";
Head->Next = NULL;

If you want to initialize the struct this way, you should define the members as pointers:
 
Code:
struct Item_Struct {   char *Text;   char *Price;   char *BMP_NUM;   char *BUT_NUM;   ...
   ...
};
 
 
But be aware that the data resides in 'write only' memory.
It depends on what you want to do with it...
 
--> pittbull
 
0 Kudos

413 Views
timmy10ar
Contributor I
Thanks for the insight. I think I may just go with the global way of doing this, (this is for a senior design project). Anyway, can I initialize by using:

typedef struct {
char Text[30];
char Price[6];
char BMP_NUM[3];
char BUT_NUM[3];
char CATEGORY[2];
char SUBCAT[2];
char POS[2];
char USED[1];
}Menu_Item;

Menu_Item Global_Set[2];

Global_Set = {
{"Cheeseburger", "$5.99", "45","45","1","1","1","y"},
{"Spinach Salad","8.96", "46", "46","2","1","1","y"}
};

?? or do i have to type it out item by item? I am having problems compiling the above code. It keeps saying "incompatible type to previous declaration found on the initialization line.... Thanks...
0 Kudos

413 Views
CompilerGuru
NXP Employee
NXP Employee
You cannot separate the initialization from the definition.
(if you do, then it gets an assignment, and same story, you cannot assign arrays in C....)

With the code below, I do get
"Warning : C3401: Resulting string is not zero terminated"
twice, both times correct. The "y" does not fit into the "char USED[1]"
field.

Daniel


typedef struct {
char Text[30];
char Price[6];
char BMP_NUM[3];
char BUT_NUM[3];
char CATEGORY[2];
char SUBCAT[2];
char POS[2];
char USED[1];
}Menu_Item;

const Menu_Item Global_Set[2]= {
{"Cheeseburger", "$5.99", "45","45","1","1","1","y"},
{"Spinach Salad","8.96", "46", "46","2","1","1","y"}
};
0 Kudos

412 Views
Ivychacha
Contributor I
It's perhaps a stupid question but i didn't know that before, in your struct definition of Item_Struct you got an element: struct Item_Struct *Next, how can we contain the struct in this own definition? Won't that create a dead lock like that?

why are you using malloc? just declare it.
0 Kudos

413 Views
CompilerGuru
NXP Employee
NXP Employee
Is this some homework task?

Anyway, in C you cannot assign arrays (or strings), you have to copy them. Use strcpy to copy the strings into their fields.
Do you have to allocate the struct with malloc? With global initialization this would be more efficient and you could even initialize the strings.

Daniel
0 Kudos