当前位置:网站首页>C语言数组
C语言数组
2022-07-04 12:33:00 【天黑再醒】
目录
一维数组:
一维数组创建:
格式:元素类型 数组名 [常量表达式]
如:int arr[10] ;
const int n=10;
int arr[n]; //由const 是常变量 本质上来说还是变量,所以不成立
注:数组创建,在C99标准之前, [] 中要给一个常量才可以,不能使用变量。在C99标准支持了变长数组的概念。
一维数组初始化:
int arr[5]={1,2,3,4} //结果是: 1 2 3 4 0 字符个数小于[]里的由0来补
int arr[]={1,2,3,4} //结果是 : 1 2 3 4
char ch[]={'a','b','c'} //结果是: a b c
char ch[]={'a',98,'c'} //结果是: a b c 因为在ASCII表里字符b的ASCII值为98
char ch[4]={'a','b','c'} //结果是: a b c \0
char ch[]="abc" //结果是: a b c \0
int arr[10]={1,2,3,4,5};printf("%d\n",arr[4]);
赋值1~10
#include <stdio.h>
int main()
{
int arr[10] = {0};
int i = 0;
for(i=0; i<10; i++)
{
arr[i] = i+1;
}
for(i=0; i<10; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
数组的大小也是可以计算的:
int arr[10];int sz = sizeof(arr);//求字节的个数int sz = sizeof(arr1[10]);//求一个元素的字节int sz = sizeof(arr)/sizeof(arr[0]);//求元素的个数
一维数组在内存中的存储:
#include <stdio.h>
int main()
{
int arr[10] = {0};
int i = 0;
int sz = sizeof(arr)/sizeof(arr[0]);
for(i=0; i<sz; ++i)
{
printf("&arr[%d] = %p\n", i, &arr[i]);
}
return 0;
}
随着数组下标的增长,元素的地址,也在有规律的递增。
二维数组:
二维数组的创建:
int arr[3][5]; //3行5列
char arr[3][5]; //3行5列double arr[2][4]; //2行4列
二维数组初始化:
int arr[3][5]={1,2,3,4,5,6};
// { 1 2 3 4 5 }
{ 6 0 0 0 0 }
{ 0 0 0 0 0 }
如果想把1,2存在第一行,3,4存在第二行,5,6存在第三行则:
int arr[3][5]={ {1,2},{3,4},{5,6}};
// { 1 2 0 0 0 }
{ 3 4 0 0 0 }
{ 5 6 0 0 0 }
二维数组初始化的时候行可以省略,列不能省略
想打印出这个
int arr[3][5]={ {1,2},{3,4},{5,6}};
代码如下:
#include <stdio.h>
int main()
{
int arr[3][5] = { {1,2},{3,4},{5,6} };
int i = 0;
for (i = 0; i < 3; i++)
{
int j = 0;
for (j = 0; j < 5; j++)
{
printf("%d", arr[i][j]);
}
printf("\n");
}
return 0;
}
二维数组的存储:
#include <stdio.h>
int main()
{
int arr[3][5];
int i = 0;
for(i=0; i<3; i++)
{
int j = 0;
for(j=0; j<5; j++)
{
printf("&arr[%d][%d] = %p\n", i, j,&arr[i][j]);
}
}
return 0; }
二维数组在内存中也是连续存放的。
数组的越界:
#include <stdio.h>
int main()
{
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
int i = 0;
for(i=0; i<=10; i++)
{
printf("%d\n", arr[i]);//当i等于10的时候,越界访问了
}
return 0;
}
数组名:
数组名是数组首元素的地址。
有两个例外:
1.sizeof(数组名),数组名不是数组首元素的地址,数组名表示整个数组,计算的是整个数组的地址。
2.&数组名, 数组名不是数组首元素的地址,数组名表示整个数组,取出的是整个数组的地址。
#include <stdio.h>
int main()
{
int arr[10] = {1,2,3,4,5,6,7,8,9,10};printf("%p\n", arr);
printf("%p\n", arr+1);printf("%p\n", &arr[0]);
printf("%p\n", &arr[0]+1);printf("%d\n", sizeof(arr));
printf("%p\n", &arr);
printf("%p\n", &arr+1);return 0;
}
冒泡排序:
数组中2个相邻的元素进行比较,如果不满足顺序就进行交换。
如: 将3,1,4,2,5,7,6,8,10,9 排序(升序)
#include <stdio.h>
//void bubble_sort(int* arr[], int sz)
void bubble_sort(int arr[], int sz)
{
int i = 0;
for (i = 0; i < 10; i++)
{
int j = 0;
for (j = 0; j < sz - 1 - i; j++)
{
if (arr[j] > arr[j + 1])
{
int tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
}
}
int main()
{
int arr[10] = { 3,1,4,2,5,7,6,8,10,9 };
int sz = sizeof(arr) / sizeof(arr[0]);
bubble_sort(arr, sz);
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ",arr[i]);
}
return 0;
}
如果顺序正确,那只是在交换,可以优化一下:
#include <stdio.h>
void bubble_sort(int arr[], int sz)
{
int i = 0;
for (i = 0; i < 10; i++)
{
int flag = 1;
int j = 0;
for (j = 0; j < sz - 1 - i; j++)
{
if (arr[j] > arr[j + 1])
{
flag = 0;
int tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
if (1 == flag)
{
break;
}
}
}
int main()
{
int arr[10] = { 3,1,4,2,5,7,6,8,10,9 };
int sz = sizeof(arr) / sizeof(arr[0]);
bubble_sort(arr, sz);
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
以上内容为数组相关的。
愿诸君共勉之!!!!
边栏推荐
- (August 9, 2021) example exercise of air quality index calculation (I)
- Global and Chinese market for naval vessel maintenance 2022-2028: Research Report on technology, participants, trends, market size and share
- The database connection code determines whether the account password is correct, but the correct account password always jumps to the failure page with wrong account password
- When synchronized encounters this thing, there is a big hole, pay attention!
- Reptile learning 3 (winter vacation learning)
- Iframe to only show a certain part of the page
- 0x15 string
- Azure solution: how can third-party tools call azure blob storage to store data?
- OSI seven layer model & unit
- MYCAT middleware installation and use
猜你喜欢
Clion configuration of opencv
JD home programmers delete databases and run away. Talk about binlog, the killer of MySQL data backup
OSI seven layer model & unit
【数据聚类】第四章第一节3:DBSCAN性能分析、优缺点和参数选择方法
R语言--readr包读写数据
netstat
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 11
Servlet learning notes
Entitas learning [iv] other common knowledge points
(August 10, 2021) web crawler learning - Chinese University ranking directed crawler
随机推荐
Lvs+kept highly available cluster
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 15
Openssl3.0 learning 20 provider KDF
C language compilation process
How to create a new virtual machine
[ES6] template string: `string`, a new symbol in es2015
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 9
French Data Protection Agency: using Google Analytics or violating gdpr
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 8
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 10
[solve the error of this pointing in the applet] SetData of undefined
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 7
Force buckle 142 Circular linked list II
Global and Chinese markets of NOx analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
2018 meisai modeling summary +latex standard meisai template sharing
C language memory layout
Flet教程之 按钮控件 ElevatedButton入门(教程含源码)
Detailed explanation of NPM installation and caching mechanism
Iframe to only show a certain part of the page
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 6