定义一个结构体
typedef struct _t1
{
int a;
int b;
char* c;
}t1;
两个相同的t1结构体之间怎么传递地址?
t1 a t1 b;
怎么把a的指针传给b;
这样? &b=&a?????
typedef struct _t1
{
int a;
int b;
char* c;
}t1;
两个相同的t1结构体之间怎么传递地址?
t1 a t1 b;
怎么把a的指针传给b;
这样? &b=&a?????
解决方案
30
#include <stdio.h> typedef struct _t1 { int a; int b; char* c; }t1; t1 a={1,2,"a"},b; int main() { b=a; printf("b.a,b.b,b.c:%d,%d,%s\n",b.a,b.b,b.c);//b.a,b.b,b.c:1,2,a return 0; }
10
试试memcpy?
#include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct _t1 { int a; int b; char* c; }t1; void main() { t1 a = {1, 2, "sdf"}; t1 b = {4, 5, "sdf"}; t1 c; printf("%d-%d-%s\n", a.a, a.b, a.c); printf("%d-%d-%s\n", b.a, b.b, b.c); memcpy(&c, &a, sizeof(t1)); memcpy(&a, &b, sizeof(t1)); memcpy(&b, &c, sizeof(t1)); printf("%d-%d-%s\n", a.a, a.b, a.c); printf("%d-%d-%s\n", b.a, b.b, b.c); }