问:&在我这个程序中的作用

来源:百度知道 编辑:UC知道 时间:2024/07/03 01:00:20
int initStack(SqStack &s){
s.base=(int*)malloc(STACK_INIT_SIZE*sizeof(int));
if(!s.base)exit(-2);
s.top=s.base;
s.stacksize=STACK_INIT_SIZE;
return OK;
}
在这句中int initStack(SqStack s)的&符号,我如果将&去掉,就会出错,为什么勒?&在这里起什么作用,我记得有一次老师讲线性表的时候给我们说&符号可有可无啊!
下面是完整的程序,将程序中所有的&都去掉,程序也不对了,给我具体说一下&的作用嘛

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

using namespace std;

#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OK 1
#define ERROR 0
#define OVERFLOW -2

typedef struct
{ int * base;
int * top;
int stacksize;
}SqStack;

int initStack(SqStack &s){
s.base=(int*)malloc(STACK_INIT_SIZE*sizeof(int));
if(!s.base)exit(-2);
s.top=s.base;
s.stacksize=STACK_INIT_SIZE;
return OK;
}

int push(SqStack &S, int e)
{ if(S.top-S.base

就是引用,看c++的引用把

看C++ 的引用语法

typedef struct
{ int * base;
int * top;
int stacksize;
}SqStack;
结构体中的是指针变量,所以要用&,我说的对吗?

int pop(SqStack &S, int &e) &是引用,