0

i am receiving a default char array (first/last) in guest_init (and i need to initialize the values such that guest have default values ) is my following code correct? as when i run this g->first_name is always being assigned garbage. need some help.

struct guest {
    char last_name[30];
    char first_name[30];
};
struct auditorium_seating {
    struct guest **seating;
};
void guest_init_default(struct guest *g)
{

    *g->first_name = "???";
    *g->last_name = "???";
}
void guest_init(struct guest *g, char *info)
{
        strcpy(g->first_name, strtok(info, "/"));
        strcpy(g->last_name, strtok(NULL, "\0"));




}
void auditorium_seating_init(int rowNum, int columnNum, struct auditorium_seating *a)
{
    a->seating=malloc((sizeof(a->seating[rowNum][columnNum])));
    char string_arr[30]="aaa/bbb";
    for (int i = 0; i<rowNum; i++)
    {
        for (int j = 0; j<columnNum; j++)
        {



            //guest_init_default(a->seating);
            guest_init(a->seating,string_arr);


        }
    }

}

auditorium_seating_init being called from main.

void main() {
    struct auditorium_seating auditorium_seating;
    struct guest temp_guest;
    int row, col, rowNum, columnNum;
    char guest_info[30];

    printf("Please enter a number of rows for an auditorium seating.");
    scanf_s("%d", &rowNum);

    printf("Please enter a number of columns for an auditorium seating.");
    scanf_s("%d", &columnNum);

    auditorium_seating_init(rowNum, columnNum, &auditorium_seating);

    printf("Please enter a guest information or enter \"Q\" to quit.");
}

2 Answers2

3

Enable your compiler warnings: *g->first_name = "???"; is wrong.

And strtok(NULL, "\0")); is wrong too.

You probably want this:

#include <string.h>
#include <stdio.h>

struct guest {
  char last_name[30];
  char first_name[30];
};

void guest_init(struct guest *g, char *info)
{
  strcpy(g->first_name, strtok(info, "/"));
  strcpy(g->last_name, strtok(NULL, "/"));
}

int main()
{
  struct guest g;
  char info[] = "Foo/Bar";
  guest_init(&g, info);
  printf("Last Name = %s\n", g.last_name);
  printf("First Name = %s\n", g.first_name);
}

There may be more errors related to struct auditorium_seating *a, but you didn't post that code.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • sorry about that but i have added the rest now – Deathknight921 Sep 19 '17 at 07:31
  • 2
    @AbdulKhan: This answer solves your problem and you should be able to fix the rest of your code too. Your question is about very basic stuff of the C language: pointers, C-strings, dynamic memory allocation etc. Further the problems you have can all be found on with google and on StackOverflow already answered. You should take a [good C book](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) and learn the basics. – Andre Kampling Sep 19 '17 at 07:34
  • @AbdulKhan there are other problems in your code, show how you call `auditorium_seating_init` including the declarations of the variables involved. – Jabberwocky Sep 19 '17 at 07:36
-2

No, you must copy data to your structure, as you allocated memory inside it.

Read compiler errors and use it to fix your program.

bukkojot
  • 1,526
  • 1
  • 11
  • 16