当前位置:网站首页>C language foundation day4 array
C language foundation day4 array
2022-07-19 03:00:00 【LuZhouShiLi】
C Language foundation Day4- Array
One 、 To define an array
Store several variables of the same data type in a continuous memory , All members in the same array have the same data type , At the same time, the addresses of all members in memory are consecutive .
Two 、 Declare an array
- The type of each element in the array is determined by the type in front of the array name
- The number of elements in the array is determined by [] The value inside determines
- When defining an array ,[] The value inside cannot be a variable , It can only be constant
- Using arrays ,[] The value inside can be a constant or a variable
- Numeric arrays cannot be manipulated as a whole
- Every element of the array is a variable
int num[10];
int num[10] = {
1,2,3,4,5,6,7,8,9,10};
// If the array initializes only some elements , Other elements are initialized to 0
int num[10] = {
1,2};
int num[10] = {
0};// Initialize all elements of the array to 0
int num[10] = {
[5] = 5};// Mark the subscript as 5 The element of is assigned the value of 5
int num[] = {
1,2,3};// [] No value in The number of elements in this array is determined by {} The number of elements inside determines
3、 ... and 、 Length of array
int num[10] = {
0};
int n = sizeof(num) / sizeof(num[0]);// The length of the array
Four 、 How to store arrays in memory
Start a program , The system will allocate a piece of memory space to the program ; The smallest unit of memory is a byte , Every byte in memory has a number , This number is the address in memory .
Address of data in memory , Is its starting address in memory .
such as ,int b, Occupy four bytes of memory , Then the addresses occupied by these four bytes are :0x1001、0x1002、0x1003、0x1004, that b The memory address of is the starting address :0x1001.

- explain :
- a[0] The first 0 Elements
- &a[0] The first 0 Addresses of elements == 01
- Array name a Representative array , Also stands for the 0 Addresses of elements a == &a[0] == 01, So the array name is a constant , It's an address .
- &a Get the address of the entire array == 01 In numerical terms &a[0],a,&a, equal , But the meaning is different
- &a[0] + 1 Add one to the address of the element Across an element == 05
- a+1 Add one to the address of the element Across an element == 05
- &a + 1 Add one to the address of the entire array , Across the entire array == 21
#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:4996)
int main()
{
int a[5];
printf("%u\n",&a[0]);
printf("%u\n",a);// Array name It's the address of the first element
printf("%u\n",&a);// Take out Address of array
printf("%u\n",&a[0] + 1);// The first element of the array Add one Across an element + 4
printf("%u\n",a + 1);// Add one to the address of the element Across an element
printf("%u\n",&a + 1);// Add one to the address of the entire array Across the entire array
return 0;
}
5、 ... and 、 Bubble sort

Bubble sort : Compare two adjacent elements , The front one is bigger than the back one , Exchange two elements , If the array has n Elements , Then you need to exchange in total n - 1 round , Every time i Round exchange n - i Time .
#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:4996)
int main()
{
int a[5] = {
2,5,6,3,-1};
/*printf("%u\n",&a[0]); printf("%u\n",a);// Array name It's the address of the first element printf("%u\n",&a);// Take out Address of array printf("%u\n",&a[0] + 1);// The first element of the array Add one Across an element + 4 printf("%u\n",a + 1);// Add one to the address of the element Across an element printf("%u\n",&a + 1);// Add one to the address of the entire array Across the entire array */
int n = sizeof(a) / sizeof(a[0]);// Calculate how many elements the array has Divide the length of all bytes of the array by the length of a single element
for (int i = 0; i < n - 1; i++)
{
// The outer loop It mainly controls the number of cycles
// Inner circulation mainly controls the comparison times of each cycle
for (int j = 0; j < n - i - 1; j++)
{
if (a[j] > a[j+1])
{
// Exchange elements
int t = a[j];
a[j] = a[j+1];
a[j+1] = t;
}
}
}
for (int i = 0; i < n; i++)
{
printf("%d ",a[i]);
}
return 0;
}
6、 ... and 、 Two dimensional array

- A two-dimensional array is conceptually two-dimensional : Its subscript changes in two directions , Access to it usually requires two subscripts
- There is no two-dimensional array in memory , The actual hardware memory of two-dimensional array is continuously addressed , That is, there are only one-dimensional arrays in memory , That is, put one line in sequence after the second line , It's the same way as a one-dimensional array .
- int a[2][4] Defines a two-dimensional array , A two-dimensional array has two one-dimensional , Each one-dimensional array has four elements , Then the two-dimensional array a[0] On behalf of the 0 That's ok
6.1 Initialization of an array
#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:4996)
int main()
{
int a[3][4] = {
{
1,2,3,4},{
5,6,7,8},{
9,10,11,12} };// Initialization of 2D array
// If you initialize some elements of a two-dimensional array , The other elements are 0
int b[3][4] = {
1,2,3 };
int c[3][4] = {
1,2,3,4,5,6,7,8,9,10,11,12};// Two dimensional arrays are essentially one-dimensional arrays Subscripts can be calculated automatically
int d[][3] = {
1,2,3,4,5 };// When defining a two-dimensional array The subscript of the column cannot be omitted The subscript of the line can be omitted The subscript of a row can be calculated automatically
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d ",d[i][j]);
}
}
return 0;
}
6.2 Calculate the number of rows and columns of the array
- Calculate the total number of array elements :sizeof(a) / sizeof(a[0][0]);
- Calculate the number of rows of the array :sizeof(a) / sizeof(a[0]); a[0] Represents the size of a row of elements
- Calculate the number of columns in the array :sizeof(a[0]) / sizeof(a[0][0]); Divide the number of bytes of a line of elements by the number of bytes of an element
#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:4996)
int main()
{
int a[3][4] = {
{
1,2,3,4},{
5,6,7,8},{
9,10,11,12} };// Initialization of 2D array
int n = sizeof(a) / sizeof(a[0][0]);// Calculate the total number of elements
int row = sizeof(a) / sizeof(a[0]);// Calculate the number of rows of the array Divide the size of a two-dimensional array by the size of a row
int column = sizeof(a[0]) / sizeof(a[0][0]);// Calculate the number of columns in the array Divide the size of a row by the size of an element
printf("%d %d %d\n",n,row,column);
return 0;
}
6.3 Array name

int a[2][3];
* a[0][0] The first 0 Xing di 0 Elements
* &a[0][0] The first 0 Xing di 0 Addresses of elements
* a[0] On behalf of the 0 Array name of row one-dimensional array , Also stands for the 0 Address of row one-dimensional array a[0] = &a[0][0]
* &a[0] The first 0 The address of the line = 01
* a Array name of two-dimensional array Represents a two-dimensional array , Also represents the first line address &a[0]
* &a Address of two-dimensional array
* &a[0][0] + 1 Element address plus 1, Across an element
* a[0] + 1 Add one to the element address , Across an element
* &a[0] + 1 Line address plus one , Cross a line
* a + 1 Line address plus one , Cross a line
* &a + 1 Two dimensional array address plus one , Across the entire array
#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:4996)
int main()
{
int a[3][4] = {
{
1,2,3,4},{
5,6,7,8},{
9,10,11,12} };// Initialization of 2D array
int n = sizeof(a) / sizeof(a[0][0]);// Calculate the total number of elements
int row = sizeof(a) / sizeof(a[0]);// Calculate the number of rows of the array Divide the size of a two-dimensional array by the size of a row
int column = sizeof(a[0]) / sizeof(a[0][0]);// Calculate the number of columns in the array Divide the size of a row by the size of an element
printf("%d %d %d\n",n,row,column);
printf("%u\n",&a[0][0]);
printf("%u\n",a[0]);
printf("%u\n",&a[0]);
printf("%u\n",a);
printf("%u\n",&a);
printf("%u\n",&a[0][0] + 1);// Just add an element address
printf("%u\n",a[0] + 1);// Just add the address of one element
printf("%u\n",&a[0] + 1);// Add the address of a line of elements
printf("%u\n",a + 1);// Add the address of a line of elements
printf("%u\n",&a + 1);// Add the element address of the entire array
return 0;
}
7、 ... and 、 Multidimensional arrays
int num[2][3][4]; Defines a three-dimensional array , There are two two-dimensional arrays , Each two-dimensional array has three one-dimensional arrays , Each one-dimensional array has four elements .
8、 ... and 、 A character array
A string is an array of characters with \0 An array of characters , Because there is \0 A character array of characters , Easy to operate , Numbers 0( character ’\0’ Of ACSII code ) At the end of the char An array is a string , But if chaR The array is not numbered 0 ending , Then it's not a string , Just an ordinary character array , So string is a special kind of char Array .
#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:4996)
int main()
{
char a[5] = {
'a','b','c','d','\0'};// The character array contains \0 Character It's just a string
char b[5] = "abcd";// Defines a character array Deposit is abcd\0
char c[] = "world";// The length of the array is 6 character string
char d[100] = "abc";// Defines a character array Yes 100 Elements The other elements are \0
char e[100] = {
0};// Clear a character array 0 0 Is the character '\0' Of acsii code
for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++)
{
printf("%d ",a[i]);// Single print character Print in decimal form
}
printf("\n");
// Character arrays can also be printed as strings Just until the first address of the array
printf("%s\n",a);
printf("%s\n",b);
printf("%s\n",c);
printf("%s\n",d);
printf("%s\n",e);// All spaces
return 0;
}
#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:4996)
int main()
{
char a[128] = "abcd\0def\0";
printf("%d\n",sizeof(a));// 128
printf("%s\n",a);// Print abcd
return 0;
}
#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:4996)
int main()
{
char a[] = {
'a','b','c'};// An ordinary character array
printf("%d\n",sizeof(a));// Output 3
printf("%s\n",a);// No characters 0 ending Output abc Scald The statement
return 0;
}
#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:4996)
int main()
{
char a[] = {
'a','b',0,'c'};// There is 0 Express '\0'
char b[] = {
'a','b','0','c',0};// In the middle of the 0 Characters will be printed because acsii yes 48
char c[] = {
'a','b','c','\0','v'};// Print abc
printf("%s\n",a);// Print ab
printf("%s\n",b);// Print ab0c
printf("%s\n",c);// Print abc
return 0;
}
Nine 、scanf Input string
At the end of a carriage return , It ends when you encounter a space , meanwhile , If the space for storing read characters is insufficient , Continue to want to store characters later , No, ’\0’ character , When printing, all characters will be printed directly , Even if it exceeds the length of the character array , This causes memory pollution
#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:4996)
int main()
{
char num[128] = "";// Character array clear 0
scanf("%s",num);// Get a string from the keyboard At the end of a carriage return There is no need to take the address character
printf("%s\n",num);// %s What you want is to print the address of the first element of the character array
return 0;
}
Ten 、gets function
#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:4996)
int main()
{
// gets Function encountered \n end But it won't end when encountering spaces Read spaces But it will also cause memory pollution
char num[128] = "";// Character array clear 0
gets(num);// The parameter is the address where the read string is stored
printf("num = %s\n",num);
return 0;
}
11、 ... and 、fgets() function
Library function : Read a string from the keyboard ;char num[128]; fgets(num,sizeof(num),stdin);// fgets from stdin( The standard input - keyboard ) Read string to num Array , It can be read at most sizeof(num) - 1 Characters , Because you need to add ’\0’;
- How to reset the last character to ’\0’
#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:4996)
int main()
{
char buf[128] = "helloA";
// Set the last character of the string to '\0'
// First you need to find the subscript of the last character
// Find the number of valid characters of the character array
int i = 0;
while (buf[i] != '\0')
{
i++;
}
// Last i Dropped the subscript 6 The location of
printf("%d\n",i);
buf[i - 1] = '\0';// Set the last character to '\0'
printf("%s\n",buf);// Print hello
return 0;
}
Use strlen() Function to calculate the number of valid characters of a character array , Then set the last character to ’\0’
#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:4996)
#include<stdlib.h>
#include<string.h>
int main()
{
char buf[128] = "helloA";
// Set the last character of the string to '\0'
// First you need to find the subscript of the last character
// Find the number of valid characters of the character array
int i = strlen(buf);// strlen() Calculate the number of valid characters in the character array
buf[i - 1] = '\0';// Set the last character to '\0'
printf("%s\n",buf);// Print hello
return 0;
}
#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:4996)
#include<stdlib.h>
#include<string.h>
int main()
{
//fgets Will read the Enter key
char buf[1024] = "";
fgets(buf,sizeof(buf),stdin);// Read newline
buf[strlen(buf) - 1] = '\0';// Set the newline character directly to '\0'
printf("%s\n",buf);// Print hello
return 0;
}
Twelve 、 Output of character array
- puts、fputs function
#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:4996)
#include<stdlib.h>
#include<string.h>
int main()
{
char buf[1024] = "helloworld";
puts(buf);// Automatically output a line feed
fputs(buf,stdout);// The first parameter , The address of the first element of the array stdout standard output ( The screen )
return 0;
}
边栏推荐
- 梦开始的地方----初识c语言
- 3、AsyncTool框架原理源码解析
- 需要慢一点点
- Oracle gets the last and first data (gets the first and last data by time)
- Rhce8 Study Guide Chapter 7 service management
- HCIA_ Nat experiment
- Binary installation kubernetes 1.23.2
- What happens when you get stuck compiling and installing MySQL database in Linux system?
- 【单片机仿真】(十九)介绍汇编、汇编指令、伪指令
- OpenVINO中的FCOS人脸检测模型代码演示
猜你喜欢

Yum warehouse service and PXE automatic deployment system

1. Introduction, analysis and implementation of asynctool framework

Oracle查询时间段内所有日期

Configure VLAN and use OSPF protocol for layer 3 switches
4. Some thoughts on asynctool framework

Advanced usage of the responsibility chain pattern

仿射变换实现

5. Is the asynctool framework flawed?

當你在Linux系統中編譯安裝MySQL數據庫卡住了怎麼辦?

5、AsyncTool框架竟然有缺陷?
随机推荐
微信小程序
Comprehensive experiment of static routing
【单片机仿真】(五)寻址方式 — 立即寻址与寄存器间接寻址
HCIA's first static routing experiment
Nat comprehensive experiment
多项式插值拟合(一)
【单片机仿真】(二)keil 安装教程
mysqldump: [Warning] Using a password on the command line interface can be insecure.
多项式插值拟合(三)
MySQL数据库中的事务和存储引擎
快照:数据快照(数据兜底方式)
使用gatekeeper限制kubernetes创建特定类型的资源
【NoSQL】NoSQL之redis配置与优化(简单操作)
Detailed explanation of dynamic compression and static compression of gzip
SSH Remote Control and access
【单片机仿真】(十四)指令系统位操作类指令 — 位数据传送指令MOV、位变量修改指令
【单片机仿真】(八)指令系统 — 数据传送指令
仿射变换实现
RHCE8学习指南第2章 基本命令的使用
二进制安装kubernetes 1.23.2