Dragon
Dragon

Reputation: 2481

C: addressing to inner structure in linked list

I have a structure that is going to be a linked list. But at first I cannot read values from inner structes. It sounds complicated, but here is the code:

#include <stdio.h>
#include <stdlib.h>

struct Address
{
    char city[50];
};

struct Task
{
    char fullName[255];
    struct Address address;
};

struct TaskList
{
    struct Task* task;
    struct TaskList* next;
};

struct Task createTask()
{
    struct Task task;
    struct Address address;
    printf("Enter full name: ");
    scanf("%s", task.fullName);
    printf("Enter the city: ");
    scanf("%s", address.city);
    task.address = address;

    return task;
}

void addTask(struct TaskList *head)
{
    struct TaskList* temp;
    struct Task task = createTask();
    temp->task = &task;
    temp->next = head;
    head = temp;
}

int main()
{
    struct TaskList *head;
    head = NULL;
    addTask(head);
    printf("%s", head->task->address.city);

    return 0;
}

When I launch the app and enter some data, the process crashes with the following code:

Process returned -1073741819 (0xC0000005) execution time : 11.102 s

How in fact should I address inner structures and their fields?

Upvotes: 0

Views: 106

Answers (4)

pts
pts

Reputation: 87211

There are multiple memory issues with this code, here is the explanation and the solution for one of them.

Valgrind shows:

==1249== Invalid read of size 8
==1249==    at 0x40051B: main (/tmp/t.c:48)
==1249==  Address 0x0 is not stack'd, malloc'd or (recently) free'd

It looks like that the head is still null when you print it in line 48. addTask doesn't change the local variable head in main. Fix it like this:

struct TaskList* addTask(struct TaskList *head)
{
    struct TaskList* temp = malloc(sizeof *temp);
    struct Task* task = malloc(sizeof *task);
    *task = createTask();
    temp->task = task;
    temp->next = head;
    return temp;
}
int main()
{
    struct TaskList *head;
    head = NULL;  
    head = addTask(head);
    printf("%s", head->task->address.city);
    return 0;
}

Upvotes: 0

Michael
Michael

Reputation: 1503

You should pass head pointer by poiner to addTask() to change it inside it:

struct TaskList* addTask(struct TaskList **head)

And set it in it:

*head = temp;

And createTask() must explore malloc() and for scanf() calls pointers to variables must be used:

struct Task *createTask()
{
    struct Task *task = malloc(sizeof(struct Task));
    printf("Enter full name: ");
    scanf("%s", &task->fullName);
    printf("Enter the city: ");
    scanf("%s", &task->address.city);
    return task;
}

As Address structure is a part of Task - there is only one call to malloc().

Upvotes: 1

Ali Kazmi
Ali Kazmi

Reputation: 1458

struct Address
{
    char city[50];
};

struct Task
{
    char fullName[255];
    Address address;
 };

struct TaskList
{
    Task* task;
    TaskList* next;
 };

 struct Task createTask()
 {
    Task task;
    Address address;
    printf("Enter full name: ");
    scanf("%s", task.fullName);
    printf("Enter the city: ");
    scanf("%s", address.city);
    task.address = address;

   return task;

}

Also your "head" pointer is null, intialize it properly

Try using it this way remove keyword "struct" , it must be working fine

Upvotes: 0

louxiu
louxiu

Reputation: 2915

The task created by createTask is allocated on stack, it will be freed after the function(createTask) ends. Use malloc instead.

Upvotes: 2

Related Questions