结构体赋值

作者:追风剑情 发布于:2020-1-20 10:34 分类:C

示例

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdbool.h>

#define LEN 20

struct names {
	char first[LEN];
	char last[LEN];
};

struct guy {
	struct names handle; //嵌套结构
	char favfood[LEN];
	char job[LEN];
	float income;
};

int main(int argc, char* argv[])
{
	// 初始化一个结构变量
	struct guy fellow = {
		{ "Ewen", "Villard" },
		"grilled salmon",
		"personality coach",
		68112.00
	};
	//结构体直接赋值
	struct guy fellow1 = fellow;//内存复制(按值传递)
	fellow.job[0] = 'X';
	printf("first=%s, last=%s\n", 
		fellow1.handle.first, fellow1.handle.last);
	printf("favfood=%s, job=%s\nincome=%.2f\n", 
		fellow1.favfood, fellow1.job, fellow1.income);



	system("pause");
	return 0;
}

运行测试

1111.png

标签: C语言

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号