我对c中的结构的了解有限,我所知道的是你可以为结构的成员赋值,而不是为结构本身赋值。所以我的主要问题是,如果它也是指针变量,是否允许为结构变量本身赋值? 例如:顶部=p;
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct stack
{
int no;
struct stack *prev;
} *top = NULL;
typedef struct stack st;
void push();
void push()
{
st *p;
p = (int *)malloc(sizeof(struct stack));
printf("\nEnter the value : ");
scanf("%d", &p->no);
p->prev = top;
top = p;
}
top
具有类型 struct stack *
,因此将另一个具有该类型的变量的值分配给它是完全可以的,在本例中为 p
。
您实际上可以一次分配整个结构。例如:
struct s {
int a;
int b;
};
int main()
{
struct s x,y;
x.a = 2;
x.b = 3;
y = x;
return 0;
}