当前位置:网站首页>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;
}
以上内容为数组相关的。
愿诸君共勉之!!!!
边栏推荐
- Workplace liquor bureau must pay attention to
- French Data Protection Agency: using Google Analytics or violating gdpr
- Global and Chinese markets for environmental disinfection robots 2022-2028: Research Report on technology, participants, trends, market size and share
- World document to picture
- Process communication and thread explanation
- 13、 C window form technology and basic controls (3)
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 5
- Mongodb vs mysql, which is more efficient
- Snowflake won the 2021 annual database
- TCP slicing and PSH understanding
猜你喜欢
Alibaba cloud server connection intranet operation
How to use "bottom logic" to see the cards in the world?
Entitas learning [3] multi context system
Single spa, Qiankun, Friday access practice
Clion configuration of opencv
Memory computing integration: AI chip architecture in the post Moorish Era
TCP slicing and PSH understanding
When synchronized encounters this thing, there is a big hole, pay attention!
LVS load balancing cluster deployment - Dr direct routing mode
Btrace tells you how to debug online without restarting the JVM
随机推荐
Fastlane 一键打包/发布APP - 使用记录及踩坑
JD home programmers delete databases and run away. Talk about binlog, the killer of MySQL data backup
Decrypt the advantages of low code and unlock efficient application development
Iframe to only show a certain part of the page
Global and Chinese market of ice water machines 2022-2028: Research Report on technology, participants, trends, market size and share
Entitas learning [iv] other common knowledge points
Global and Chinese market of dental elevators 2022-2028: Research Report on technology, participants, trends, market size and share
记一次 Showing Recent Errors Only Command /bin/sh failed with exit code 1 问题
Complementary knowledge of auto encoder
template<typename MAP, typename LIST, typename First, typename ... Keytypes > recursive call with indefinite parameters - beauty of Pan China
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 11
How do std:: function and function pointer assign values to each other
TCP fast retransmission sack mechanism
First knowledge of spark - 7000 words +15 diagrams, and learn the basic knowledge of spark
OSI seven layer model & unit
Ml and NLP are still developing rapidly in 2021. Deepmind scientists recently summarized 15 bright research directions in the past year. Come and see which direction is suitable for your new pit
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 17
Recommend a cool geospatial data visualization tool with low code
The frost peel off the purple dragon scale, and the xiariba people will talk about database SQL optimization and the principle of indexing (primary / secondary / clustered / non clustered)
Process communication and thread explanation