Consider the error in following program-

#include<stdio.h>
#include<conio.h>
#define MAX 100

struct stacktype
{
int stack[MAX];
int top;
}
;


void push(struct stacktype * s,int item)
{

if(s->top==MAX-1)
{
printf("Overflow");
return;
}

s->top++;
s->stack[s->top]=item;

}


int pop(struct stacktype *s)
{
int item;
if(s->top==-1)
{
printf("UnderFlow");
return -1;

}
item=s->stack[s->top];
s->top--;
return item;

}

Though I know the fault that pointers are being passed by value.. but don't understand that when i pass s1(pointer), the address of the structure being pointed to should get copied to s.. and then changing s->top should also change s1's top.. (both pointing to same structure).. Please clarify I am a bit confused..

Are you initialising s.top to 0 before you try to do top++ or top-- ?

I am really sorry.. I missed some code above.. Here is the entire code:-

#include<stdio.h>
#include<conio.h>
#define MAX 100

struct stacktype
{
int stack[MAX];
int top;
}
;


void push(struct stacktype * s,int item)
{

if(s->top==MAX-1)
{
printf("Overflow");
return;
}

s->top++;
s->stack[s->top]=item;

}


int pop(struct stacktype *s)
{
int item;
if(s->top==-1)
{
printf("UnderFlow");
return -1;

}
item=s->stack[s->top];
s->top--;
return item;

}

void show(struct stacktype* s)
{
while(s->top!=-1)
{
printf(" %d ",pop(s));
}
printf("\n");
}

void initstack(struct stacktype *s)
{
   s->top=-1;

    }


int main()
{
int choice,num;
struct stacktype *s1;
//s1->top=-1;
initstack(s1);
do
{
scanf("%d",&num);
push(s1,num);
printf("\nMore Elements?(1/0):  ");
scanf("%d",&choice);


}while(choice !=0);

printf("Contents of stack1:  ");
show(s1);

}

Please learn to format your code properly. It's very difficult to follow.

Your problem is that you have declared a pointer to a stacktype structure (on line 62), which I guess you wanted to do so that you can pass it to your functions. This is fine, but you need to point it at an actual stacktype structure. So, something like:

stacktype myStack;       // The actual structure (has the data in it)
stacktype *myStack_ptr;  // A pointer to a stacktype structure (just a memory address)
myStack_ptr = &myStack;  // Aim the pointer at your structure of choice
initstack(myStack_ptr);
push(myStack_ptr,1);     // Now pass the pointer to the function

Alternatively, you could pass the address of your structure of interest directly:

stacktype myStack;       // declare a stacktype structure
initstack(&myStack);     // pass the address of the structure directly to the function
push(&myStack,1);        // Use the address again in another function

Both of these are equivalent (I think, someone correct me if I'm wrong).

Also, you don't need

struct stacktype s1;

on line 62, you can just use stacktype s1 .

Also, you should be careful because your function show() doesn't just show the stack, it changes it dramatically, by removing all the values from it! This would lead to problems if people were confused by the name and thought that it just printed out the values, which they would. I would recommend that you either call it something different, like " popPrint() " or make it only shows the values and not alter them.

Finally, can I echo WaltP's suggestion that you format your code in a more readable fashion!

Also, you don't need

struct stacktype s1;

on line 62, you can just use stacktype s1 .

Only in C++. Please don't do that in C.

Only in C++. Please don't do that in C.

Sorry, my bad!

Sorry, my bad!

Well, since you're repentant, I won't call the Standards Police... ;)

C++ mixed in with C is one of my pet peeves. Sorry for sounding snippy.

Thanks all for the replies.I understood my mistake and for the code I would just say it wasn't my code, my friend had a doubt and he wanted me to ask it here.. But will surely format any code I put in here.. Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.