结构体数组的赋值
struct book{};struct book a[100];
struct book temp={};//初始化并赋值
能不能直接a[i]=temp
如果可以的话,这是把a[i]的地址指向temp的地址还是和int类型一样,只給值
#include<stdio.h> #include<stdlib.h> int main() { struct book{ int no; int *p; }a[2]; int i; a[0].no=0; a[0].p=(int *)malloc(sizeof(int)); *a[0].p=1; a[1]=a[0]; printf("the a[0]'s address is %p\n",a[0].p); printf("the a[1]'s no is:%d\n",a[1].no); printf("the a[1]'s p 's value is %d\n",*(a[1].p)); printf("the a[1]'s address is %p\n",a[1].p); return 0; }结果是:
#include <stdio.h> #include <stdlib.h> int main(void) { struct data{int *p;} a, b; a.p = (int *)malloc(sizeof(int)); *(a.p) = 0; b = a; printf("a.p = %p\n", a.p); printf("a.p = %d\n", *(a.p)); printf("b.p = %p\n", b.p); printf("b.p = %d\n", *(b.p)); *(b.p) = 1; printf("a.p = %p\n", a.p); printf("a.p = %d\n", *(a.p)); printf("b.p = %p\n", b.p); printf("b.p = %d\n", *(b.p)); free(b.p); printf("a.p = %p\n", a.p); printf("a.p = %d\n", *(a.p)); printf("b.p = %p\n", b.p); printf("b.p = %d\n", *(b.p)); *(b.p) = 1; printf("a.p = %p\n", a.p); printf("a.p = %d\n", *(a.p)); printf("b.p = %p\n", b.p); printf("b.p = %d\n", *(b.p)); b.p = NULL; printf("a.p = %p\n", a.p); printf("a.p = %d\n", *(a.p)); printf("b.p = %p\n", b.p); printf("b.p = %d\n", *(b.p)); *(b.p) = 1; printf("a.p = %p\n", a.p); printf("a.p = %d\n", *(a.p)); printf("b.p = %p\n", b.p); printf("b.p = %d\n", *(b.p)); return 0; }