//
// main.c
// 数组基本概念
//
// Created by xiaomage on 15/6/9.
// Copyright (c) 2015年 itcast. All rights reserved.
//
#include <stdio.h>
int main(int argc, const char * argv[]) {
// 当需要保持很多"相同类型"的数据时就可以使用数组来保持
// 因为如果使用变量, 那么将变得非常复杂, 并且非常没有技术含量
int score = 99;
int score1 = 98;
...
int score998 = 88;
printf("%i\n", score);
/*
数组的定义格式:
数据类型 变量名称;
数据类型 数组名称[数据的个数];
元素类型 数组名称[元素个数];
元素类型: 就是数组中需要存储的数据类型, 一旦指定, 数组中就只能存储该类型的数据
元素个数: 就是数组中能够存储的数据(元素)的个数
*/
int num;
num = 12;
printf("num = %i\n", num);
int scores[3]; // 定义了一个名称叫做scores的数组, 数组中可以存放3个int类型的数据
// scores = 12; // 系统搞不清楚应该赋值给谁
// 只要定义一个C语言的数组, 系统就自动会给数组中的每一块小得存储空间一个编号
// 这个编号从0开始, 一次递增
// 数组中系统自动绑定的编号, 我们称之为 索引
scores[0] = 12;
scores[1] = 66;
scores[2] = 59;
printf("scores[0] = %i\n", scores[0]);
return 0;
}
//
// main.c
// 数组的初始化和遍历
//
// Created by xiaomage on 15/6/9.
// Copyright (c) 2015年 itcast. All rights reserved.
//
#include <stdio.h>
int main(int argc, const char * argv[]) {
// 需求保持全班101个人的分数
// 元素类型 数组名称[元素个数];
/*
// 先定义再初始化
int scores[5];
scores[0] = 99;
scores[1] = 88;
scores[2] = 77;
scores[3] = 66;
scores[4] = 100;
*/
/*
int num;
num = 10;
int num1 = 10;
*/
// 依次将{}中的每一个值赋值给数组中的每一个元素
// 并且从0开始赋值
// 也称之为数组的初始化(完全初始化)
int scores[5] = {99,88,77,66,100};
// 部分初始化
// 默认从0开始初始化, 依次赋值
// 注意: 如果"在部分初始化中"对应的内存没有被初始化, 那么默认是0
int scores1[3] = {11, 22};
printf("0 = %i\n", scores1[0]);
printf("1 = %i\n", scores1[1]);
printf("2 = %i\n", scores1[2]);
printf("-------\n");
// 注意: 如果没有对数组进行初始化(完全和部门), 那么不要随便使用数组中的数据, 可能是一段垃圾数据(随机值)
int scores2[3];
printf("0 = %i\n", scores2[0]);
printf("1 = %i\n", scores2[1]);
printf("2 = %i\n", scores2[2]);
printf("-------\n");
// 注意: 定义数组的时候, 数组的元素个数不能使用变量, 如果使用变量, 那么数组中是一些随机值
int num = 10;
int scores3[num];
printf("0 = %i\n", scores3[0]);
printf("1 = %i\n", scores3[1]);
printf("2 = %i\n", scores3[2]);
// 注意: 不建议使用变量定义数组, 如果使用了变量定义数组, 作为数组的元素个数, 不初始化的情况下是随机值, 如果初始化会直接报错
// int num2 = 10;
// int scores4[num2] = {11, 12};
printf("-------\n");
// 注意: 如果定义的同时进行初始化, 那么元素的个数可以省略
// 省略之后, 初始化赋值几个数据, 那么数组的长度就是几. 也就是说数组将来就能存储几个数据
int scores5[] = {1, 3};
printf("0 = %i\n", scores5[0]);
printf("1 = %i\n", scores5[1]);
printf("-------\n");
// 注意; 如果定义数组时没有进行初始化, 那么不能省略元素个数
// int scores6[];
// 0 1 2 3 4
// int socres7[101] = {0, 0, 0, 1, 3};
// int socres7[101];
// socres7[99] = 1;
// socres7[100] = 3;
// 可以通过[索引] = 的方式, 给指定索引的元素赋值
int socres7[101] = {[99] = 1, [100] = 3};
printf("3 = %i\n", socres7[99]);
printf("4 = %i\n", socres7[100]);
// 注意: 只能在定义的同时利用{}进行初始化, 如果是先定义那么就不能使用{}进行初始化
// 如果先定义那么就不能再进行整体赋值, 只能单个赋值
// int scores8[3];
// scores8 = {1 , 4, 19};
// scores8[0] = 1;
return 0;
}
//
// main.c
// 数组的遍历
//
// Created by xiaomage on 15/6/9.
// Copyright (c) 2015年 itcast. All rights reserved.
//
#include <stdio.h>
int main(int argc, const char * argv[]) {
// 取出数组中所有的值, 称之为遍历
int scores[6] = {1, 23, 44, 66, 71, 88, 99 , 2};
/*
printf("scores[0] = %i\n", scores[0]);
printf("scores[1] = %i\n", scores[1]);
printf("scores[2] = %i\n", scores[2]);
printf("scores[3] = %i\n", scores[3]);
printf("scores[4] = %i\n", scores[4]);
printf("scores[5] = %i\n", scores[5]);
*/
// 注意: 在遍历数组的时候, 尽量不要把遍历的次数写死
// 遍历多少次应该由数组来决定, 也就是说遍历多少次应该通过数组计算得出
/*
printf("scores = %lu\n", sizeof(scores)); // 计算出数组占用的总字节数
printf("scores[0] = %lu\n", sizeof(scores[0])); // 计算出数组中某一个元素占用的字节数
printf("一个有多少个元素 : %lu\n", sizeof(scores) / sizeof(scores[0]));
*/
// 动态计算数组的元素个数
int length = sizeof(scores) / sizeof(scores[0]);
for (int i = 0; i < length; i++) {
printf("scores[%i] = %i\n", i,scores[i]);
}
return 0;
}