C语言基础3

使用数组可以使问题更简单明了,下面是各数组的用法以及举例

一维数组:

例如:#include<stdio.h>
int main(void) {
int a[4];
a[0] = 1;
a[1] = 2;
printf("%d\n", a);
printf("%d\n", &a[0]);
printf("%d\n", a[1]);

二位数组:

例如:二维数组的遍历

#include<stdio.h>
int main(void) {
int a[2][3] = {{100, 99, 60}, {99, 60, 99}};
int i, j;
for (i = 0; i < 2; i++) {
for (j = 0; j < 3; j++) {
if (i == 0) {
printf("小明的成绩:%d\n", a[i][j]);
}
if (i == 1) {
printf("小勇的成绩:%d\n", a[i][j]);
}
}
}

字符串数组:

例如:

#include<stdio.h>
#include<string.h>
int main(void) {
// int a[2][2][3] = {{{1, 2 ,3}, {1, 2, 3}}, {{1, 2, 3}, {1, 2, 3}}};
char b[] = "china";
int a[] = {1, 2, 3};
char c = 65;
//%d c可输出为数字 
//%c c也可输出为字符 
printf("%s\n", b);// 如果按%s字符串格式输出,则为字符串 
printf("%d\n", b);//如果首地址按%d整形输出,则为地址 
printf("%d\n", a);

char national[2][100] = {"China", "Japan"};

//数组再存数组,便是二位数组 
int grade[2][2] = {{99, 100}, {88, 66}};
//数组存数组再存一个数组,便是三维数组(字符串是数组) 
char users[2][2][8] = {{"lili", "123"},{"hihi", "123"}};

char p[] = "123";
char pp[] = "123";
char ppp[] = "1234";
printf("%d\n", p == pp);//p和pp是首地址,两个数组的首地址不一样 
printf("%d\n", strcmp(p, pp) == 0);//strcmp是比较的两个字符串(经过处理的) 
printf("%d\n", strcmp(p, ppp) == 0);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值