0

I got a void **stack which contains the addresses of the struct pointers my problem is that when I print the whole stack different addresses are shown than the actual addresses I'm actually trying to save in the stack.

I've tested if stud_input works correctly by printing the content of the struct and it seems to work fine.

I normally have 1 more struct(struct professor) but I got the same prob for both so I'm not posting both to make it less complicated.

example of what I expect to be printed vs what I really get when I print the addresses

Expected:

5836544 
5836851 
5837158

What i really get:

5836544
5836545
5836546

part of the code

struct student
{
    char flag;
    char surname[256];
    int semester;
};

main()
{

    struct student *stud;
    //....
    menu(stack,stackcopy,reversedstack,&prof,&stud,stacksize,&head);
    //....


}

void menu(void **stack,void **stackcopy,void **reversed,struct professor **ptr1,struct student **ptr2,int size,int *head)
{
     //....
     input(stack,ptr1,ptr2,size,head,&a,&b);
     //.....
}

int input(void **stack,struct professor **ptr1,struct student **ptr2,int size,int     *head,int *a,int *b)
{
    // *a and *b are variables that increase by one each time a student registration happens to change the add of the struct pointer every time
    int i,done=0,stud_or_prof=-1;
    //....
    *(ptr2+*a)=(struct student *)malloc(sizeof(struct student));
    done=Push(stack,head,(*(int *)ptr2+*a),size);

    if (done==1)
        stud_input(ptr2,a);
    else
        free(*(ptr2+*a));
    //...
}

int Push(void **stack,int *H,int element,int size)
{
    if (*H==(size))
        return 0;

    *((int *)stack+*H)=element;
    (*H)++;

    return 1;
}
MattSt
  • 1,024
  • 2
  • 16
  • 35
  • 2
    Can you please come up with a smaller example that still has the same issue? That way you can post the entire source code without having to edit anything out. Also please show the output you expect, and the output you actually get. – turbulencetoo Mar 31 '14 at 12:59
  • In `input` function, you first `push` the ptr2 and then you allocate memory to it. – γηράσκω δ' αεί πολλά διδασκόμε Mar 31 '14 at 13:02
  • only thing u actually have to see is input function and push function menu and main are shown only for you to see how i pass by reference the address of the struct pointer.I tried what valter said and i still have the same prob.Just posted an example of what i expect vs what i really get – MattSt Mar 31 '14 at 13:07

0 Answers0