Here is an array implementation of stack I'm trying to accomplish.
#include<stdio.h>
#include<stdlib.h>
#define MAX 5
typedef struct
{
int arr[MAX];
int top;
}STACK;
//#typedef struct stack STACK;
//---------------------- PUSH and POP ----------------
//push----
void push(STACK &s, int num)
{
if (s.top == MAX-1)
{
printf("\n\nstack full\n");
return;
}
else
{
s.top = s.top + 1;
s.arr[s.top] = num;
}
return;
}
//pop----
int pop(STACK &s)
{
int num;
if(s.top == -1)
{
printf("\n\nStack is empty\n");
return s.top;
}
else
{
num = s.arr[s.top];
s.top = s.top -1;
}
return num;
}
// ---main
int main()
{
STACK s;
int popped;
s.top = -1;
push(s, 1);
printf("%d \n",s.top);
push(s, 2);
printf("%d \n",s.top);
push(s, 3);
printf("%d \n",s.top);
popped = pop(s);
popped = pop(s);
popped = pop(s);
popped = pop(s);
printf("%d \n",popped);
return 0;
}
When in int main()
, if I do
push(&s,VALUE)
I'm passing by reference, and I can use
void push(STACK *s, int VALUE)
to dereference it. The program works fine.
However, when I do call-by-reference like:
push(s, VALUE)
and in the call,
void push(STACK &s, int value)
I get error saying "too many arguments to function call, expected 1, have 2"
What am I doing wrong? Isn't the call-by-reference correct?