Kuper
Kuper

Reputation: 83

How do I change change the variables of a structure inside an array

I'm trying to figure something out. I've created a new struct called guest:

typedef struct guest {
    char guest_name[GUEST_AND_ROOM_NAME_LENGTH];
    int money_left;
    bool is_guest_in_room;
}guest;

And I've written a function to initialize the variables of each struct in an array of guest structs. I have initialized the arrays like so:

guest *guest_list[MAX_GUESTS_IN_HOTEL];

and my function for updating the parameters is:

void create_new_guest(char *guest_line_in_file, guest **guest_list, int num_of_guests_in_hotel) 
{
    char *line_ptr = (char*)malloc((strlen(guest_line_in_file)+1) * sizeof(char));
    char *split_string[NUM_OF_PARAMETERS_FOR_GUEST]; 

    strcpy(line_ptr, guest_line_in_file);
    if ((guest_list != NULL)&&(line_ptr!=NULL)) {
        split_string_by_space(line_ptr, split_string);
        strcpy((guest_list[num_of_guests_in_hotel])->guest_name, split_string[0]);
        (guest_list[num_of_guests_in_hotel])->is_guest_in_room = false;
        (guest_list[num_of_guests_in_hotel])->money_left = atoi(split_string[1]);
    }
    else{ 
        printf("Memory allocation of new guest failed! Exiting\n"); 
    }
    free(line_ptr);
    return;
}

The call for the function:

create_new_guest(current_line_in_file, guest_list, num_of_guests_in_hotel);

Where current_line_in_file is a string that contains the input of the variables and is decomposed by split_string_by_space function.

But every time it gets to the part of updating the name of the guest I get: 'Access violation writing location 0xCCCCCCCC.'

I know there's something wrong with how I initialize my arrays but I don't quite understand what is it.

Thanks in advance.

Upvotes: 1

Views: 65

Answers (2)

1.

guest_list, after:

  guest *guest_list[MAX_GUESTS_IN_HOTEL];

as well as,

split_string, after:

  char *split_string[NUM_OF_PARAMETERS_FOR_GUEST];

are not arrays of the respective datatypes of guest and char, as you might have intended to do. They are arrays of pointers after these declarations.

Omit the dereference operator *:

  guest guest_list[MAX_GUESTS_IN_HOTEL];

  char split_string[NUM_OF_PARAMETERS_FOR_GUEST];

Also edit the parameter guest **guest_list to guest *guest_list in the call of create_new_guest(). Then test it again.

2.

You shouldn´t use the same identifier for the structure name/tag and a variable of its own:

typedef struct guest {
    char guest_name[GUEST_AND_ROOM_NAME_LENGTH];
    int money_left;
    bool is_guest_in_room;
}guest;                           // <- Same Name for variable as structure

Even if this is valid in C, it is still bad programming style. Rather do it like that:

typedef struct guest_struct {     // <- Structure tag is different now.
    char guest_name[GUEST_AND_ROOM_NAME_LENGTH];
    int money_left;
    bool is_guest_in_room;
}guest;

Technically, it is also allowed to omit a separate structure name/tag by define structures by the use of typedef:

typedef struct{     //<- Structure tag is omitted here now.
    char guest_name[GUEST_AND_ROOM_NAME_LENGTH];
    int money_left;
    bool is_guest_in_room;
}guest;

Upvotes: 1

Dave Costa
Dave Costa

Reputation: 48111

You are never allocating memory to store the actual guest data structure.

The guest_list declaration allocates an array that can hold the given number of pointers. It does not allocate the memory that the pointers will reference.

Upvotes: 2

Related Questions