confusion with passing address

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?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  #include <stdio.h>
#include <stdlib.h>

struct arraystack
{
int top;
unsigned capacity;
int *array;
};
struct arraystack* createstack(int cap)
{
struct arraystack *stack;
stack=(struct arraystack*)malloc(sizeof(struct arraystack));
stack->capacity=cap;
stack->top=-1;
stack->array=(int*)malloc(sizeof(int)*cap);
return stack;
}
int isfull(struct arraystack*stack)
{
if(stack->top==stack->capacity-1){return 1;}
else{return 0;}
}
int isempty(struct arraystack *stack)
{
if(stack->top==-1){return 1;}
else{return 0;}
}
void push(struct arraystack *stack,int item)
{
if(!isfull(stack))
{
stack->top++;
stack->array[stack->top]=item;
}
}
int pop(struct arraystack *stack)
{
int item;
if(!isempty(stack))
{
item=stack->array[stack->top];
stack->top--;
return(item);
}
else{return 0;}
}
int main()
{
    struct arraystack *stack;int item,choice;
    stack=createstack(4);
    while(1)
    {
    printf("\n1. push");
    printf("\n2. pop");
    printf("\n3. exit");
    printf("\n Enter your choice ");
    scanf("%d",&choice);
    switch(choice)
    {
    case 1:
    printf("\n Enter a number : ");
    scanf("%d",&item);
    push(stack,item);//this line*****************************
    break;

    case 2:
    item=pop(stack);
    if(item!=-1){printf(" poped value is : %d ",item);}
    else{printf("\n stack is empty");}


    case 3:
    exit(0);

    }
    return 0;
    }
}
Last edited on
x4.cc:17:1: error: return-statement with no value, in function returning ‘arraystack*’ [-fpermissive]
You have a function that's not returning a value.

Your indentation makes your code difficult to read.
sorry for the typing mistake,but now the code runs,please clear my confusion now
please clear my confusion now
Do you realize that the infinite while loop in main() never loops? If your indentation was better, it would be obvious.

Do you realize that if option 2 is selected the program terminates? If your indentation was better, it would be obvious.

You shouldn't expect someone to decipher the mass of code above main() as it stands.
The code is now simplified.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  #include <stdio.h>
#include <stdlib.h>

struct arraystack
{
int top;
unsigned capacity;
int *array;
};
struct arraystack* createstack(int cap)
{
struct arraystack *stack;
stack=(struct arraystack*)malloc(sizeof(struct arraystack));
stack->capacity=cap;
stack->top=-1;
stack->array=(int*)malloc(sizeof(int)*cap);
return stack;
}


void push(struct arraystack *stack,int item)
{

stack->top++;
stack->array[stack->top]=item;
}

int main()
{
    struct arraystack *stack;int item;
    stack=createstack(4);
    scanf("%d",&item);
    push(stack,item);//this line*****************************
    
    return 0;
    
}

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?
both passes address to the function?
No. arraystack is a pointer to a record, a struct.

I'm not sure what you're asking, so I've documented the code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#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;
}
Last edited on
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.
1
2
3
4
5
6
7
8
void destroystack(struct arraystack*& stack)
{
    if (stack) {
        free(stack->array);
        free(stack);
        stack = nullptr;
    }
}
Thanks again
If you're programming in C, the cast after malloc is actually not recommended, based on what I've read on other forums.
Thanks Ganado.
Topic archived. No new replies allowed.