1、一维数组初始化:

    (1) int k[4] = {1,2,3,4}; 即k[0]=1; k[1]=2; k[2]=3; k[3]=4;

    (2)int array[4] = {0}; 即array[0]=0; array[1]=0; array[2]=0; array[3]=0; (注:数组初始化值的个数小于元素个数时,剩余的元素自动初始化为0,也就是这里的array[1]、array[2]和array[3])

    (3)int a[] = {1,2,3,4}; 可以不指定元素个数,编译器会根据列出的初始化值的个数来确定数组的元素个数。


2、二维数组的初始化:eg:int array[3][2] = {{1,2}, {3,4}, {5,6}}; 等价于 int array[][2] = {{1,2}, {3,4}, {5,6}};


3、结构体1:


// lesson1_array.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "string.h"


struct Person
{
	char szName[12];
	int nAge;
	int bSex;
};

int _tmain(int argc, _TCHAR* argv[])
{
	Person person1;
	// 不能用“=”直接赋值
	strcpy(person1.szName, "Kaven");
	person1.nAge = 22;
	person1.bSex = 1;

	char tempSex[4];
	if (person1.bSex == 1)
	{
		strcpy(tempSex, "男");
	}
	else
	{
		strcpy(tempSex, "女");
	}

	printf("%s是个%d岁的%s孩!\n", person1.szName, person1.nAge, tempSex);

	return 0;
}



数组 结构体_i++





4、结构体2:


struct Stu_course
{
	char szName[20];
	float cChinese;
	float cMath;
	float cEnglish;
};

int _tmain(int argc, _TCHAR* argv[])
{
	Stu_course stucou[3];
	// 获取成绩
	for(int i = 0; i < 3; i++)
	{
		printf("请输入第%d个学生的信息(姓名 语文 数学 英语):", i + 1);
		scanf("%s %f %f %f", &stucou[i].szName, &stucou[i].cChinese, &stucou[i].cMath, &stucou[i].cEnglish);
	}
	// 成绩处理
	float total_ch = 0.0;
	float total_ma = 0.0;
	float total_en = 0.0;

	for (int i = 0; i < 3; i++)
	{
		total_ch += stucou[i].cChinese;
		total_ma += stucou[i].cMath;
		total_en += stucou[i].cEnglish;
	}

	// 平均成绩
	printf("\n");
	printf("语文平均成绩为:%.2f\n", total_ch / 3.0);
	printf("数学平均成绩为:%.2f\n", total_ma / 3.0);
	printf("英语平均成绩为:%.2f\n", total_en / 3.0);
	printf("\n");

	// 总成绩
	float total = 0.0;
	for (int i = 0; i < 3; i++)
	{
		total += stucou[i].cChinese + stucou[i].cMath + stucou[i].cEnglish;
		printf("学生%s的总分为:%f\n", stucou[i].szName, total);
		total = 0.0;
	}

	return 0;
}



数组 结构体_初始化_02