运行时检查失败#3 - 正在使用变量'name_t'而未进行初始化

时间:2016-11-02 09:38:44

标签: c linked-list

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

这是我宣布的结构

刚刚输入学生编号后,当我按下回车键时,它给出了错误“运行时检查失败#3 - 变量'name_t'正在使用而未被初始化。”

struct node
{
    int data;
    int number;
    char name, transaction;
    struct node *next;

}*newNode, *list, *temp;

void insertNode();
void displayNode();
void deleteNode();

下面是我写的插入函数

void insertNode()
{
    int number_t;
    char name_t, transaction_t;

    newNode = (struct node*) malloc(sizeof(struct node));

    printf("Enter Student number: ");
    scanf("%d", &number_t);

    newNode->number = number_t;
    newNode->next = NULL;

    printf("Enter Student name: ");
    scanf("%s", name_t);

    newNode->name = name_t;
    newNode->next = NULL;

    printf("Enter Transcation Desciption: ");
    scanf("%s", transaction_t);

    newNode->transaction = transaction_t;
    newNode->next = NULL;


    if (list == NULL)
        list = newNode;
    else
    {
        temp = list;
        while (temp->next != NULL)
        {
            temp = temp->next;
        }
        temp->next = newNode;
    }
}

1 个答案:

答案 0 :(得分:2)

scanf("%s", name_t);

scanf期待一个字符串,但是你传递一个普通的char(没有足够的空间来存储字符串)

我建议fgets以避免缓冲区溢出:

char name[100]; /* name_t is not a good name for a variable */
char *ptr;

if (fgets(name, sizeof name, stdin)) {
    /* strip the trailing newline */
    ptr = strchr(name, '\n');
    if (ptr != NULL) {
        *ptr = '\0';
    }
}