我试图根据以下开头(stack.h )在c实现堆栈。 设置指针以将c语言中的堆栈初始化为空
# ifndef堆叠_ h
#define STACK_H
/* anelementfromwhichstackisconsisting * /
typedef struct stack_node_ss {
结构堆栈_ node _ ss * next;/* pointertonextelementinstack * /
void *value;/* This element * /
}堆叠_节点_ s;
/* typedefsothatstackuserdoesn ' thavetoworryabouttheactualtypeof
* parameterstackwhenusingthisstackimplementation。
*/
typedef stack_node_s* stack_s;
/* initializesastackpointedbyparameterstack.usercallsthisafterhe
* hascreatedastack _ tvariablebutbeforeheusesthestack。
*/
void堆栈_ init (堆栈_ s *堆栈);
/* pushesitemtoastackpointedbyparameterstack.returns0if succesful,
*-1以太网。
*/
intstack_push(void*p,stack_s *stack );
/* popsitemfromastackpointedbyparameterstack.returnspointerto
* elementremovedfromstackifsuccesful,null if there is an error or
* the堆叠is empty。
*/
void *堆栈_ pop (堆栈_ s *堆栈);
#endif
但是,作为新的乐队c,我只停留在stack_init功能上,写在stack.c上:
#包含
#包含
#include 'stack.h '
void堆栈_ init (堆栈_ s *堆栈) {
(*堆栈)-value=NULL;
(*堆叠) -下一步=空;
}
主程序开头:
int *tmp;
堆叠_ s堆叠;
堆叠_ init (堆叠;
这样的崩溃与我的程序:
programreceivedsignalexc _ bad _ access,could not access存储器。
reason : kern _ invalid _ addressataddress 33600 x 000000000008
0x 000000010000 abfinstack _ init (堆叠=0x7ff F5 fbffb 30 ) at stack.c:6
6 (堆叠) -值=空值;
你能暗示我走上正确的轨道吗? 非常感谢。
2012-02-22
rize
2
所以,除非真的有好的理由,否则我不会隐藏typedefs后面的指针类型。
2012-02-22 23:01:48
0
@Ed S .完整。 ' typedef struct {.} mystruct_t;' 但是有问题。 坦率地说。 为什么这个做法还在学校教? 我认为老师们被帕斯卡主义的终极形式所束缚。
2012-02-22 23:22:02
0
@wildplasser :呃……我写c的时候,到处都有' struct foo f;' 定义结构以避免写。 我不认为这有问题,是指针型的……有龙。
2012-02-23 00:09:49