打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
由于未分配内存的指针导致段错误

代码如下

#include<iostream>
using namespace std;

void f(char **s) {
*s = "chenyu";
}

typedef struct _node {
int val;
struct _node *next;
} Node;


typedef struct _link {
Node *front;
Node *rear;
} Link;

void queue(Link *link) {
//link.front = link.rear = NULL; //这个地方不是.而是-> 请注意
link->front = link->rear = NULL;
}

int main() {
  cout<<1<<endl;
  char *s;
  char **a;
  a = (char **)malloc(sizeof(char **));
  *a = "chenyu";
  f(&s);
  cout<<s<<endl;
  cout<<*s<<endl;
  cout<<*a<<endl;
  char *b = {"hello"};
  printf("%p\n",b);
  printf("%s\n",b);
  cout<<*b<<endl;
  Link *link;
 // link = (Link *)malloc(sizeof(Link *));
  queue(link);
  return 0;
}

编译没错

然后运行就出问题了

然后加上

 link = (Link *)malloc(sizeof(Link *));

就可以了
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
链栈
union的用法
单链表的建立(C语言完整程序)
malloc
关于union和struct的内存分配
C 内存管理详解
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服