C使用结构将char数组传递给函数

时间:2015-11-18 13:48:56

标签: c arrays pointers struct

我们有一个小程序,输入来自文本文件(参见下面的文本示例),用于扫描并将某些学生信息写入不同的结构变量。但是,当调用 add_student()函数时,它会给出这个bizzare输出(见下面的截图)。

curl -u username:password -X GET -H 'Content-Type: application/json' "http://jiraServer/rest/api/2/search?jql=created%20>%3D%202015-11-18"

Input text file sample

here is the output

在突出显示的部分之间// ------------------- 在代码中,我们可以从第一个printf()看到正确的名称输出,但是当我们转到第二个printf()时,它只打印空白区域......你知道可能是什么问题吗?

输入文字文件:

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;
        }
    }
}

控制台输出:

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

1 个答案:

答案 0 :(得分:4)

studentnode = malloc(sizeof(struct student));

仅为student的实例分配内存。尚未分配studentnode->d的内存。因此n->d->something中的任何add_student()都是无效的,因此会调用未定义的行为。