当前位置: 技术问答>linux和unix
求助,帮忙看看为什么无法打印数据。
来源: 互联网 发布时间:2016-12-20
本文导语: 用链表封装一个栈,push写完想测试一下入栈,结果发现在push内用 //printf("Head:%dn", *(int *)handle->head.next->data);可以打印出入栈的数据,在main里面调用push后,用 //printf("Head:%dn", *(int *)handle->head.next->data);打印却发现提...
用链表封装一个栈,push写完想测试一下入栈,结果发现在push内用 //printf("Head:%dn", *(int *)handle->head.next->data);可以打印出入栈的数据,在main里面调用push后,用 //printf("Head:%dn", *(int *)handle->head.next->data);打印却发现提示段错误,估计传参没写好,就是找不出来,各位帮帮忙。
4 struct node_t{
5 void *data;
6 struct node_t *next;
7 struct node_t *prev;
8 };
9
10 typedef struct Stack_List{
11 struct node_t head;
12 int size;
13 int max;
14 int op;
15 }stack;
int stack_list_push(stack *handle, void *data)
80 {
81 int i;
82 struct node_t *tmp;
83
84 if(handle->op == handle->max)
85 return;
86
87 struct node_t *new = NULL;
88 new = (struct node_t *)malloc(sizeof(struct node_t));
89 ERR(NULL == new, goto ERR1); //容错
90
91 new->data = (void *)malloc(handle->size);
92 ERR(NULL == new->data, goto ERR2); //容错
93
94 memcpy(new->data, data, handle->size);
95
handle->head.prev->next = new;
11
119 new->next = &handle->head;
12
121 new->prev = handle->head.prev;
12
123 handle->head.prev = new;
124
126 handle->op += 1;
127 //printf("Head:%dn", *(int *)handle->head.next->data);
128 ERR2:
129 free(new);
130 ERR1:
131 return -1;
132 }
int main(void)
12 {
13 stack *handle = NULL;
14 handle = stack_list_create(sizeof(int), 1000);
15 PRERR(1);
16 int n = 10;
17
18 stack_list_push(handle, &n);
19
20
21 printf("%dn", *(int *)(handle->head.next->data)); //打印不出来,段错误
4 struct node_t{
5 void *data;
6 struct node_t *next;
7 struct node_t *prev;
8 };
9
10 typedef struct Stack_List{
11 struct node_t head;
12 int size;
13 int max;
14 int op;
15 }stack;
int stack_list_push(stack *handle, void *data)
80 {
81 int i;
82 struct node_t *tmp;
83
84 if(handle->op == handle->max)
85 return;
86
87 struct node_t *new = NULL;
88 new = (struct node_t *)malloc(sizeof(struct node_t));
89 ERR(NULL == new, goto ERR1); //容错
90
91 new->data = (void *)malloc(handle->size);
92 ERR(NULL == new->data, goto ERR2); //容错
93
94 memcpy(new->data, data, handle->size);
95
handle->head.prev->next = new;
11
119 new->next = &handle->head;
12
121 new->prev = handle->head.prev;
12
123 handle->head.prev = new;
124
126 handle->op += 1;
127 //printf("Head:%dn", *(int *)handle->head.next->data);
128 ERR2:
129 free(new);
130 ERR1:
131 return -1;
132 }
int main(void)
12 {
13 stack *handle = NULL;
14 handle = stack_list_create(sizeof(int), 1000);
15 PRERR(1);
16 int n = 10;
17
18 stack_list_push(handle, &n);
19
20
21 printf("%dn", *(int *)(handle->head.next->data)); //打印不出来,段错误
|
在127行增加retutn 0或删除129行
|
程序顺序走的,不return一直走到底
|
第18行:stack_list_push(handle, &n);
这个函数的 第一个参数应该使用指针的指针才能传入正确的值:
stack **handle = NULL;
stack_list_push(&handle, &n);
,将原型和调用出都改掉试试吧
这个函数的 第一个参数应该使用指针的指针才能传入正确的值:
stack **handle = NULL;
stack_list_push(&handle, &n);
,将原型和调用出都改掉试试吧
|
你也可以在第18行加一个打印信息看看handle指针是否为NULL
printf(“handle = %pn”,handle);
你在打印处出的错估计就是指针的问题
printf(“handle = %pn”,handle);
你在打印处出的错估计就是指针的问题