当前位置:网站首页>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;
}
以上内容为数组相关的。
愿诸君共勉之!!!!
边栏推荐
- vim 出现 Another program may be editing the same file. If this is the case 的解决方法
- 【数据聚类】第四章第一节3:DBSCAN性能分析、优缺点和参数选择方法
- DDS-YYDS
- Star leap plan | new projects are continuously being recruited! MSR Asia MSR Redmond joint research program invites you to apply!
- Bottom Logic -- Mind Map
- 记一次 Showing Recent Errors Only Command /bin/sh failed with exit code 1 问题
- Process communication and thread explanation
- Global and Chinese market of dental elevators 2022-2028: Research Report on technology, participants, trends, market size and share
- IPv6 experiment
- 13、 C window form technology and basic controls (3)
猜你喜欢

Star leap plan | new projects are continuously being recruited! MSR Asia MSR Redmond joint research program invites you to apply!

Single spa, Qiankun, Friday access practice

vim 出现 Another program may be editing the same file. If this is the case 的解决方法

SAP ui5 date type sap ui. model. type. Analysis of the display format of date

Here, the DDS tutorial you want | first experience of fastdds - source code compilation & Installation & Testing

Detailed explanation of classic process synchronization problems

Wechat video Number launches "creator traffic package"
![[solve the error of this pointing in the applet] SetData of undefined](/img/19/c34008fbbe1175baac2ab69eb26e05.jpg)
[solve the error of this pointing in the applet] SetData of undefined

Force buckle 142 Circular linked list II

记一次 Showing Recent Errors Only Command /bin/sh failed with exit code 1 问题
随机推荐
How do std:: function and function pointer assign values to each other
8.8.1-PointersOnC-20220214
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 13
Iterm tab switching order
Global and Chinese markets of NOx analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
Source code analysis of the implementation mechanism of multisets in guava class library
2018 meisai modeling summary +latex standard meisai template sharing
Force buckle 142 Circular linked list II
DVC use case (VI): Data Registry
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 15
C语言:求100-999是7的倍数的回文数
Error: Failed to download metadata for repo ‘AppStream‘: Cannot download repomd. XML solution
. Does net 4 have a built-in JSON serializer / deserializer- Does . NET 4 have a built-in JSON serializer/deserializer?
Unity performance optimization reading notes - Introduction (1)
Ternsort model integration summary
Anti clockwise rotation method of event arrangement -- PHP implementation
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 9
Global and Chinese markets for soluble suture 2022-2028: Research Report on technology, participants, trends, market size and share
The solution of permission denied
Kivy教程之 08 倒计时App实现timer调用(教程含源码)