两个结构体通过结构体指针传值

/*
	结构体之间通过指针传值 
*/

#include <stdio.h>
#include <string.h>

#define SALEN (sizeof(struct structA))
#define SBLEN (sizeof(struct structB))

typedef struct structA
{
	int a;
	char b[20];
	int c;
}*SA;

typedef struct structB
{
	char a[20];
	long b;
	char c;
}*SB;


/*
	将sb->a传给sa->b 
*/
int main(void)
{
	int temp=0;
	SA sa=(SA)malloc(SALEN);
	SB sb=(SB)malloc(SBLEN);
	system("color F4"); 
	printf("输入一串字符:");	
	scanf("%s",&(sb->a)); 
	printf("\n输入字符串:【%s】",sb->a);
	printf("\n开始转换,按1确认:");
	scanf("%d",&temp); 
	if(temp==1)
	{
		memcpy(&sa->b,&sb->a,sizeof(sb->a));
		printf("\n从sb->a 到 sa->b");
		printf("\n拷贝完成,结果是:【%s】\n\n",sa->b);
	} 
}

 运行结果:

  

 

posted on 2017-01-19 10:39  HelloWorldTotti  阅读(576)  评论(0编辑  收藏  举报

导航