0xC0000005: 写入位置 0x01300000 时发生访问冲突。
 #include <stdio.h>
#include <stdlib.h>
#define MAX 20

struct node
{
    char strings[MAX];
    struct node *next;
};

struct node *creat(struct node *head)
{
    int i;
    int counter = 1;
    char input[MAX];
    struct node *new;
    struct node *pointer;

    printf("请向链表中存入第%d个字符串\n", counter);
    scanf_s("%s", input);
    for (i = 0; i < MAX; i++)
    {
        head->strings[i] = input[i];
    }
    head->next = NULL;
    pointer = head;
    counter++;
    while (counter <= 10)
    {
        new = (struct node*)malloc(sizeof(struct node));
        printf("请向链表中存入第%d个字符串\n", counter);
        scanf_s("%s", input);
        for (i = 0; i < MAX; i++)
        {
            new->strings[i] = input[i];
        }
        new->next = NULL;
        pointer->next = new;
        pointer = new;

        counter++;
    }

    return head;
}

struct node *reverse(struct node *head)
{
    struct node *tail;
    struct node *next;
    struct node *pointer;

    tail = NULL;
    next = head->next;
    pointer = head;             //初始化

    while (head != NULL)
    {
        head->next = tail;      
        tail = head;
        head = next;
        next = head->next;
    }

    return tail;

}

void print(struct node* head)
{
    struct node *pointer;
    int counter = 1;
    pointer = head;

    while (pointer != NULL)
    {
        printf("第%d个节点的数据是:%s", counter, pointer->strings);
        counter++;
        pointer = pointer->next;
    }
}

int main()
{
    struct node *head;
    head = (struct node*)malloc(sizeof(struct node));

    head = creat(head);
    head = reverse(head);
    print(head);

    system("PAUSE");
    return 0;

}

运行时第一次输入数据后便出错弹出0xC0000005: 写入位置 0x01300000 时发生访问冲突。请指教!

1个回答

不同于scanf,使用scanf_s需要确定缓冲区的长度。

 scanf_s("%s", input);

应修改为

 scanf_s("%s", input,sizeof(input));

详细可参考:

https://msdn.microsoft.com/en-us/library/w40768et.aspx

用心回答每个问题,如果对您有帮助,请采纳答案好吗,谢谢!

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!