老是提示内存访问冲突是什么情况,求大神指教!
VC_CS 2015-11-28 04:28:23 编译器总出现如下提示,求指教!
0x01214FB6 处有未经处理的异常(在 创建链表.exe 中): 0xC0000005: 写入位置 0x00000000 时发生访问冲突。
#include "stdafx.h"
#include<iostream>
using namespace std;
struct list
{
int data;
struct list *next;
};
typedef struct list node;
typedef node *link;
int main()
{
link ptr,head=NULL;
int num,i;
ptr =(link)malloc(sizeof(node));
ptr =head;
cout <<"please enter 5 numbers\n";
for(i=0;i<=4;i++)
{
cin >> num;
ptr->data =num;
ptr->next =(link)malloc(sizeof(node));
if(i==4)
ptr->next =NULL;
else
ptr =ptr->next;
}
ptr =head;
while(ptr!=NULL)
{
cout <<"the vallue is \n" <<ptr->data;
ptr =ptr->next;
}
return 0;
}