In the following code at 64 no line ,does push(stack,item); and push(&stack,item);
,both passes address to the function?If yes then how is a user defined type variable is passing address without & symbol, if no then why does the code ran perfectly without receiving a address?
In the following code at 33 no line ,does push(stack,item); and push(&stack,item);
,both passes address to the function?If yes then how is a user defined type variable is passing address without & symbol, if no then why does the code ran perfectly without receiving a address?
#include <stdio.h>
#include <stdlib.h>
// Our stack data structure
struct arraystack
{
int top; // number of elements held in the stack
unsigned capacity; // number of elements we can hold in the stack
int *array; // the array we hold the elements in
};
// Create and Initialize a stack data structure
struct arraystack* createstack(int cap)
{
// Create space for the stack data structure
struct arraystack *stack;
stack=(struct arraystack*)malloc(sizeof(struct arraystack));
// Fill in (Initialize) the stack data structure
stack->capacity=cap;
stack->top=-1;
stack->array=(int*)malloc(sizeof(int)*cap);
// We're done, return the stack data structure
return stack;
}
// BUG: we never check for overflow
void push(struct arraystack *stack,int item)
{
stack->top++; // increment top in the passed int stack data structure
stack->array[stack->top] = item; // assign item to array[top] in the passed in stack data structure
}
int main()
{
struct arraystack *stack = createstack(4); // create a stack data structure with capacity 4
int item;
scanf("%d",&item); // read item
push(stack, item); // pass the stack data structure to push(), ask it to push item onto the stack
// BUG: There's no cleanup of the stack data structure.
return 0;
}
Thanks a lot for your time and indicating bugs ,That helped me to increase my understanding level for this code. But please tell me how to fix the bug mentioned 44 no line in the code that you explained with commenting lines in your reply.