69,373
社区成员
发帖
与我相关
我的任务
分享
struct student *s;
加:
s = malloc(sizeof(struct student));
原因是你的是野指针操作,需要将s指向可读可写的内存区域,比如malloc申请的堆上空间;
我估计这不是书上的源码,因为这个错误比较明显;书上可能定义的时结构体变量而非指针。
#include "stdafx.h"
#include <stdio.h>
#include <Windows.h>
struct student { int rno; char name[20]; };
void display(struct student *st);
int main()
{
struct student *s = new student();
s->rno = 10;
strcpy_s(s->name, "sunyuhong");
display(s);
delete s;
return 0;
}
void display(struct student *st)
{
printf("\nrno=%d", st->rno);
printf("\nname=%s", st->name);
}