简体   繁体   中英

C Passing char array to function using structs

We have a small program where the input comes from a text file (see text sample below) and is used to scan and write certain student information to different struct variables. However, when the add_student() function is called it gives this bizzare output (see screenshot below).

struct student_list sl;
struct teacher_list tl;

struct data {
 char *name;
 int number;
 char index;};

struct student {
 struct data *d;
 struct student *next;};

struct student_list{
 int size;
 struct student *front;
 struct student *tail;};

struct teacher{
 struct data *d;
 struct teacher *next;};

struct teacher_list{
 int size;
 struct teacher *front;
 struct teacher *tail;};

void main()
{   
    readAndLoad();

    print_students();
}
    void readAndLoad()
{
    int c;
    int i=0;
    char line[200];
    int number, semNum;
    char name[100];
    char index;

    while ((c=getchar())!=EOF)
    {
        if(c != '\n')
        {
            line[i++] = c;
            line[i] = '\0';
            /*printf("%c ", c);
            printf("%s \n", line);*/
        }else
        {
            //printf("\n");
            int j, b; 
            b = 0;

            for (j = 0; j < sizeof(line); j++) 
            {
                if (line[j] == ' ')
                ++b;
            } 

            //printf("%s \n", line);

            if (b == 2)
            {
                if (line[0] == 'S')
                {
                    sscanf(line, "S %d %s", &number, name);
                    struct student *studentnode;

                    studentnode = malloc(sizeof(struct student));
                    add_student(&studentnode, number, &name);
                } else if (line[0] == 'T')
                {
                    sscanf(line, "T %d %s", &number, name);
                    struct teacher *teachernode;

                    teachernode = malloc(sizeof(struct teacher));
                    add_teacher(&teachernode, number, &name);
                }
            }

            memset(&line[0], 0, sizeof(line));
            i=0;
        }
    }
    //printf(line);
}

void add_student(struct student *n, int student_number, char *student_name)
{
//---------------------------------------------------
    printf("%s\n", student_name);
    n->d->name = student_name;
    n->d->number = student_number;
    n->d->index = 'S';
    n->next = 0;
    printf("%s\n", n->d->name);
//---------------------------------------------------
    if (sl.size == 0)
    {
        sl.front = n;
        sl.tail = n;
        printf("%s %d \n", n->d->name, n->d->number);
    } else
    {
        sl.tail->next = n;
        sl.tail = n;
        printf("%s %d \n", n->d->name, n->d->number);
    }

    sl.size++;
    printf("Student added\n");
}

void add_teacher(struct teacher *n, int number, char *name)
{
    n->d->name = name;
    n->d->number = number;
    n->d->index = 'T';
    n->next = 0;

    if (tl.size == 0)
    {
        tl.front = n;
        tl.tail = n;
    } else
    {
        tl.tail->next = n;
        tl.tail = n;
    }

    tl.size++;
    printf("Teacher added\n");
}
void print_students()
{
    int i;
    struct student *s = sl.front;

    for (i = 0; i < sl.size; i++)
    //while (s->next != 0)
    {
        if (i == (sl.size - 2))
        {
            printf("%c %s %d", s->d->index, s->d->name, s->d->number);
        } else
        {
            printf("%c %s %d \n", s->d->index, s->d->name, s->d->number);

            s = s->next;
        }
    }
}

Input text file sample

here is the output

Between the highlighted part //------------------- in the code we can see a correct output of the name from the first printf() but when we go to the second printf() it only prints the blank space... Do you know what could be the problem?

Input text file:

S 123456 Ivan
S 654321 Georgi
T 123456 Jesper
T 123457 Ole
T 123458 Lars
T 123459 Erland
C 31 CALI1 3
C 11 WDDI1 1
C 21 SDJI2 2
E 123456 31
E 123456 11
E 654321 21
A 123456 31
A 123457 11

Console output:

Ivan

 123456
Student added
Georgi

,▒( 654321
Student added
Teacher added
Teacher added
Teacher added
Teacher added
E 0▒( 2673448E 0▒( 2673448
studentnode = malloc(sizeof(struct student));

only allocates memory for an instance of student . The memory for studentnode->d has not been allocated. Therefore any n->d->something in add_student() is invalid, thus invokes undefined behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM