0

我有一个 void **stack 包含结构指针的地址,我的问题是,当我打印整个堆栈时,显示的地址与我实际尝试保存在堆栈中的实际地址不同。

我已经通过打印结构的内容测试了 stud_input 是否正常工作,它似乎工作正常。

我通常还有 1 个结构(结构教授),但我对两者都有相同的概率,所以我不会同时发布两者以使其不那么复杂。

我期望打印的内容与打印地址时实际得到的示例

预期的:

5836544 
5836851 
5837158

我真正得到的是:

5836544
5836545
5836546

部分代码

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;
}
4

0 回答 0