直接上代码:
#include <stdio.h>
typedef struct s{
int a;
int *p;
}S;
struct ss{
int a;
int arr[2];
};
int main()
{
struct s a;
S b;
int c = 2;
struct ss x, y;
x.a = 2;
x.arr[0] = 1;
x.arr[1] = 2;
a.a = 33;
a.p = &c;
b = a;
(*b.p) ++;
b.a ++;
printf("a:\t%-2d %-2d\n", a.a, b.a);
printf("*p:\t%-2d %-2d\n", *a.p, *b.p);
y = x;
y.arr[0] ++;
printf("arr[0]: %d %d\n", x.arr[0], y.arr[0]);
}
|
输出:
a: 33 34
*p: 3 3
arr[0]: 1 2
|
由输出可知,对于指针成员,结构体赋值拷贝指针的值,而对于数组成员,则拷贝数组的所有元素,而非拷贝数组首地址。
阅读(685) | 评论(2) | 转发(0) |