关于调用函数的参数的问题
- C/C++ code
typedef int Status;typedef char SElemType;typedef struct SqStack_t{ SElemType *top; SElemType *base; Status stack_size;}SqStack, *LPSqStack;Status InitStack(SqStack S){ S.base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType)); if (!S.base) { printf("Memory allocation failure!\n"); exit(OVERFLOW); } S.top = S.base; S.stack_size = STACK_INIT_SIZE; return OK;}Status GetTopElem(SqStack S, SElemType &e){ if (S.top == S.base) { printf("The stack is empty!\n"); return (ERROR); } else { e = *(S.top - 1); return (OK); }}Status Push(SqStack S, SElemType e){ if (S.top - S.base == S.stack_size) { S.base = (SElemType *)realloc(S.base,(S.stack_size + STACK_INCREMENT * sizeof(SElemType))); S.top = S.stack_size + S.base; S.stack_size += STACK_INCREMENT; } *S.top++ = e; return OK;}Status Pop(SqStack S, SElemType &e){ if (S.base == S.top) { printf("The stack is empty!\n"); return (ERROR); } else { e = *(--S.top); return (OK); }}Status StackEmpty(SqStack S){ if (S.top == S.base) return TRUE; else return FALSE;}上面是关于栈的一些操作
问题是:
Status InitStack(SqStack S) 中的参数用结构体名 和 结构体指针名有上面区别
Status Pop(SqStack S, SElemType &e) 中SElemType &e 换成 SElemType *e 或者 SElemType e 有什么区别
相应的调用函数应该怎么传入参数 会对主调函数中的变量产生什么影响?
[解决办法]
Status InitStack(SqStack S)
参数用结构体名 和 结构体指针 区别是函数里结构体名的改变不会让调用者的数据改变,指针的话数据就会变
Status Pop(SqStack S, SElemType &e) 中SElemType &e 换成 SElemType *e 或者 SElemType e
SElemType &e和SElemType *e都是传递的指针(地址一样),会影响主调函数中的变量,区别就是SElemType &e参数不能传NULL参数 SElemType *e可以。
SElemType e不会影响主调函数中的变量,因为地址不一样。
[解决办法]
[解决办法]