BakchodBhosdiwala
BakchodBhosdiwala

Reputation: 31

How do get void pointers working as desired below in the description?

I am trying to implement a stack in C program. This is the header file of my program, Stack.h.

#ifndef STACK_H
#define STACK_H


typedef struct node {

    void *data;
    struct node *next;
}Stack;


void push(Stack **, void *);
void *pop(Stack **);
void *peek(Stack **);

#endif

This is my implementation of the functions, in Stack.c file.

#include <stdlib.h>
#include "Stack.h" 

void push(Stack **TOP, void *data) {

    Stack *PTR;

    PTR = (Stack *)malloc(sizeof(Stack));
    if(!PTR) return;

    PTR->data = data;
    PTR->next = *TOP;
    *TOP = PTR;
}

void *pop(Stack **TOP) {

    Stack *PTR;
    void *data;
    PTR = *TOP;

    *TOP = PTR->next;
    data = PTR->data;
    free(PTR);

    return data;
}

void *peek(Stack **TOP) {

    Stack *PTR;
    PTR = *TOP;

    return PTR->data;
}

This is my main.c file.

#include <stdio.h>
#include "Stack.h"

int main() {

    int count;
    Stack *TOP = NULL;


    for(count = 0; count < 100; count += 10)
        push(&TOP, &count);

    while(TOP != NULL)
        printf("%d\n", *(int *)pop(&TOP));
}

This prints out 100 11 times like this:

100
100
100
100
100
100
100
100
100
100
100 

while it should have printed this:

100
90
80
70
60
50
40
30
20
10
0

Well, I know why this is happening. Its because we are passing the address where the count variable is stored regardless of the value it contains. Hence, we get the last value pushed in the stack 10 times.

But when I modify my main.c like this:

#include <stdio.h>
#include "Stack.h"

int main() {

    Stack *TOP = NULL;
    int a = 5;
    char b = 'A';
    float c = 15.98;
    long d = 225678460;


    push(&TOP, &a);
    push(&TOP, &b);
    push(&TOP, &c);
    push(&TOP, &d);

    printf("%ld\n", *(long *)pop(&TOP));
    printf("%f\n", *(float *)pop(&TOP));
    printf("%c\n", *(char *)pop(&TOP));
    printf("%d\n", *(int *)pop(&TOP));
}

then I get the output as desired as:

225678460
15.980000
A
5

How should I modify my 1st main.c in order to get the desired output as in 2nd main.c ?

Thanks in advance for the biggest help of my programming life. This is my college project.

Upvotes: 0

Views: 214

Answers (2)

Alex D
Alex D

Reputation: 30455

Because your stack holds pointers to values, and not literal values, you have to hold those values at different addresses. If you use just one variable in a loop, you are storing all those values successively into the same address. So all the nodes on your stack are all pointing to that one address.

To get around this, you can allocate memory on the heap for each value. Like this:

for(count = 0; count < 100; count += 10) {
    int *ptr = malloc(sizeof(int));
    *ptr = count;
    push(&TOP, (void*)ptr);
}

In your case, you are exiting immediately after printing the values on the stack, so you can get away without freeing the allocated memory. It will all be freed when the program exits. But if actually doing something like this in a real program, make sure to free the memory allocated for each value when you don't need it any more. OR, more realistically, if what you want is a stack of integers, just make each node hold an int rather than a void pointer.

Upvotes: 3

Hillel
Hillel

Reputation: 331

you problem is that what stack is holding is the address of count. you are initializing your stack with an address and not a value. *(PTR->data) = *data; i think should work now it should update a value in data field and not an address. you will need to allocate memory for data field now too...

Upvotes: 0

Related Questions