0

Lets say I have a program with a struct

typedef struct Room { char* name; struct Room * North; struct Room * South; struct Room * East; struct Room * West; struct Item * itemList; char *character[10]; } Room; 

And I have to create a bunch of them, as well as dynamically allocate memory for them. As I am new to this, there might be some obvious answers that I might not have known, but how can I assign memory to these structs, while also assigning values to them at once? I know there are ways to do so by declaring a pointer and then manually assigning data using ->, but I want to use the format I have given below, as it would be difficult to assign data one value at a time.

 struct Room* room1p = (struct Room*) malloc(sizeof(struct Room)); struct Room* room2p = (struct Room*) malloc(sizeof(struct Room)); struct Room* room3p = (struct Room*) malloc(sizeof(struct Room)); struct Room room1, room2, room3; room1 = (Room){roomList[1], NULL, &room3, &room2, NULL, itemRoom1, charList[1]}; room2 = (Room){roomList[2], NULL, &room2, &room3, &room1, itemRoom2, charList[2]}; room3 = (Room){roomList[3], NULL, &room1, NULL, &room2, itemRoom3, charList[3]}; 
1
  • The C standard does not provide a facility for both dynamically allocating memory and initializing it, except calloc, which clears memory. Write a loop or a function to do this. Commented Feb 12, 2022 at 18:41

1 Answer 1

1

There is not a built-in way to do this in C. Allocating memory is a separate action from initializing said memory with a value.

You could assign the allocation and initialization to a function if you want to be able to do both in one line:

struct Room* room_alloc_and_init(char* name, struct Room* north, struct Room* south, struct Room* east, struct Room* west, struct Item* item_list, char** character) { // Allocate struct Room* new_room = (struct Room*) malloc(sizeof(struct Room)); // Initialize new_room->Name = name; new_room->North = north; new_room->South = south; new_room->East = east; new_room->West = west; new_room->itemList = item_list; new_room->character = character; return new_room; } // Later on struct Room* room1 = room_alloc_and_init(...); // Be sure to free your memory when you are done with it! free(room1); // Also free any of the pointers you passed to room_alloc_and_init // if they point to heap allocated memory 
Sign up to request clarification or add additional context in comments.

8 Comments

btw, it would be a good practice to code the destructing method alongside with the constructing method, that way memory leaks will be less frequent.
@yair koskas, good call. I've added a free() as a reminder
Thank you! I am trying it, however, when I type in the line to allocate character data, it says expression must be a modifiable lvalue. Do you know what that means, or how to fix it?
@sreesh poudyal my bad. Your struct has a pointer to a pointer (a pointer to an array), the function needs to take a char** rather than just a char*. I have updated my answer.
Also, the function won't "allocate" character or any of the North, East, South, West rooms. It will only allocate the Room struct. Your Room struct takes a pointer to those things, so you will need to allocate those and pass their pointers to the function.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.