Cutler Cox
Cutler Cox

Reputation: 33

Using malloc to create a pointer to multiple struct items

For a c programming class, I have been told to create the following structs with typedefs, in that order:

//STRUCTS
struct time {
    unsigned int hour;
    unsigned int minute;
    unsigned int second;
};//end struct time
struct date {
    unsigned int month;
    unsigned int day;
    unsigned int year;
};//end struct Date
struct event {
    char name[20];
    struct time* time;
    struct date* date;
};//end struct event

//TYPEDEFS
typedef struct time Time;
typedef struct date Date;
typedef struct event Event;

From there I'm supposed to ask for the max number of events to create, then allocate a pointer with enough memory for that many Events. Part of my work is:

Event *events;

//Ask for max number of events and allocate memory
printf("Number of events to add: ");
scanf("%d", &numEvents);
events = (Event*) malloc(sizeof(Event) * numEvents);

However, from there I'm unsure of what to do to traverse the pointer to view a specific event. I know it isn't just an array, so events[i] won't work. but beyond that, I'm lost. My (broken)function for getting an event is:

void getEvent(Event *events, int index){
    //variables
    char title[20];
    unsigned int day, month, year, hour, minute, second;
    Event tempEvent;


    //Ask for input
    printf("Input title, date, and time.\n");
    if(index == 0)
        printf("example -> title: dd/mm/yy hh:mm:ss\n");

    //Take input
    scanf("%20[^:]: %u/%u/%u %u:%u:%u", title, &day, &month, &year, &hour, &minute, &second);


    tempEvent = (Event) {*title, { hour, minute, second }, { month, day, year } };

    events[index] = tempEvent;      
}

I know that it isn't right, and I got a segmentation fault on testing.

When I compile, I get these warnings (and some repeats about similar things):

Lab4.c: In function ‘getEvent’: Lab4.c:82:2: warning: braces around scalar initializer 
tempEvent = (Event) {*title, { hour, minute, second }, { month, day, year } }; 
^ Lab4.c:82:2: note: (near initialization for ‘(anonymous).name[1]’)

Lab4.c:82:39: warning: excess elements in scalar initializer 
tempEvent = (Event) {*title, { hour, minute, second }, { month, day, year } }

Upvotes: 0

Views: 1516

Answers (1)

user3386109
user3386109

Reputation: 34839

The compiler is telling you that line 82 is a mess.

tempEvent = (Event) {*title, { hour, minute, second }, { month, day, year } };

In particular, the first member of the event structure is an array char name[20], but you attempt to initialize it with a single character *title. But the bigger problem is that time and date (in the event structure) are pointers, and you haven't allocated any memory for those pointers.

One solution is to allocate memory, and then have scanf fill in the values, like this:

void getEvent( Event *events, int n )
{
    // allocate memory
    events[n].time = malloc(sizeof(Time));
    events[n].date = malloc(sizeof(Date));
    if ( events[n].time == NULL || events[n].date == NULL )
    {   /* TODO: handle the error */ }

    //Ask for input
    printf("Input title, date, and time.\n");
    if ( n == 0 )
        printf("example -> title: dd/mm/yy hh:mm:ss\n");

    //Take input
    int count = scanf( "%20[^:]:%u/%u/%u%u:%u:%u", events[n].name,
                       &events[n].date->day, &events[n].date->month, &events[n].date->year,
                       &events[n].time->hour, &events[n].time->minute, &events[n].time->second );
    if ( count != 7 )
    {   /* TODO: handle the error */ }
}

Upvotes: 2

Related Questions