由于未分配内存的指针导致段错误
代码如下
#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 *));
就可以了
赞 (0)