当前位置:网站首页>C language - two dimensional array, pointer
C language - two dimensional array, pointer
2022-07-27 04:47:00 【Lydialyy】
Catalog
1. Definition : type Array name [ Constant expression ][ Constant expression ]
2. Access to two-dimensional arrays
1. Pointers and pointer variables
2. Defining pointer variables : Type name * Pointer variable name
3. Take address operator and value operator
One 、 Two dimensional array
1. Definition : type Array name [ Constant expression ][ Constant expression ]
- int a[6][6];// 6*6,6 That's ok 6 Column
- char b[4][5];// 4*5,4 That's ok 5 Column
- double c[6][3];// 6*3,6 That's ok 3 Column

2. Access to two-dimensional arrays
- Array name [ Subscript ][ Subscript ]
Such as :
a[0][0];// visit a No 1 Xing di 1 The elements of the column
a[1][3];// visit a No 2 Xing di 4 The elements of the column
a[3][3];// visit a No 4 Xing di 4 The elements of the column
- You also need to pay attention to the value range of the subscript , In case of cross-border access to the array .
Such as :int a[3][4]; Its “ Line subscript ” The range of phi is zero 0-2.“ Column subscript ” The range of phi is zero 0-3, An access beyond any subscript is a cross-border access .
3. Initialization of 2D array
(1) Because the two-dimensional array is stored linearly in memory , So you can write all the data in a curly bracket :
int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};The code for :
#include <stdio.h>
int main()
{
int a[3][4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
int i,j;
for(i = 0;i < 3; i++)
{
for(j = 0;j < 4;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
return 0;
}Running results :

(2) In order to more intuitively represent the distribution of elements , You can enclose the elements of each line with braces :
int a[3][4]={
{1,2,3,4},{5,6,7,8},{9,10,11,12}};perhaps :
int a[3][4]={
{1,2,3,4},
{5,6,7,8},
{9,10,11,12}
};(3) A two-dimensional array can also assign initial values to only some elements :
int a[3][4]={
{1},{5},{9}};The code for :
#include <stdio.h>
int main()
{
int a[3][4] = {
{1},{5},{9}};
int i,j;
for(i = 0;i < 3; i++)
{
for(j = 0;j < 4;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
return 0;
}Running results :

(4) If you want the entire two-dimensional array to be initialized to 0, Then write a... Directly in braces 0 that will do :
int a[3][4]={0];If it is not initialized , Then the read value is random , as follows :
#include <stdio.h>
int main()
{
int a[3][4];
int i,j;
for(i = 0;i < 3; i++)
{
for(j = 0;j < 4;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
return 0;
}Running results :

(5)C99 Also added a new feature : Specifies the initialized element . In this way, only some specified elements in the array can be initialized and assigned , Elements that are not assigned values are automatically initialized to 0:
int a[3][4] = {[0][0] = 1,[1][1] = 2,[2][2] = 3};The code for :
#include <stdio.h>
int main()
{
int a[3][4] = {[0][0] = 1,[1][1] = 2,[2][2] = 3};
int i,j;
for(i = 0;i < 3; i++)
{
for(j = 0;j < 4;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
return 0;
}Running results :

(6) Initialization of two-dimensional arrays can also be lazy , Let the compiler calculate the length of the array according to the number of elements . But only the second 1 The number of elements of a dimension can be omitted , Other dimensions must be written :
int a[][4]={
{1,2,3,4},{5,6,7,8},{9,10,11,12}};4. Code combat : Enter the grades in the table below , And realize matrix transpose

The code is as follows :
#include <stdio.h>
int main()
{
int a[4][5] = {
{80,92,85,86,99},
{78,65,89,70,99},
{67,78,76,89,99},
{88,68,98,90,99}
};
// Enter the score
int i,j;
for(i = 0;i < 4; i++)
{
for(j = 0;j < 5;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("\n");
// Matrix transposition
for(i = 0;i < 5; i++)
{
for(j = 0;j < 4;j++)
{
printf("%d ",a[j][i]);
}
printf("\n");
}
return 0;
}Running results :

Two 、 The pointer (*)
1. Pointers and pointer variables
- Popular speaking , Pointer means address .C There are special pointer variables in the language to store pointers , It is different from ordinary variables , The pointer variable holds the address , Ordinary variables store data .
- Pointer variables have types , Its type is the data type pointed to by the stored address
2. Defining pointer variables : Type name * Pointer variable name
- char *pa;// Define a pointer variable that points to a character type
- int *pb;// Define a pointer variable that points to an integer
3. Take address operator and value operator
- If you need to get the address of the base variable , You can use the address operator (&):
char *pa=&a;
int *pb=&f;- If you need to access the data pointed to by the pointer variable , You can use the value operator (*):
printf("%c,%d\n", *pa, *pb);The code for :
#include <stdio.h>
int main()
{
char a = 'F';
int f = 123;
char *pa = &a;
int *pb = &f;
printf("a = %c\n", *pa);
printf("f = %d\n", *pb);
*pa = 'C';
*pb += 1;
printf("now,a = %c\n", *pa);
printf("now,f = %d\n", *pb);
printf("the addr of a is : %p\n", pa);
printf("the addr of b is : %p\n", pb);
return 0;
}Running results :

Be careful : Avoid accessing uninitialized pointers !!
边栏推荐
- 结构型模式-外观(Facade)模式
- Using JSON type to realize array function in MySQL
- [day02] Introduction to data type conversion, operators and methods
- Pinia uses plug-ins for persistent storage.
- 第4章 Bean对象的作用域以及生命周期
- els_ 画矩形、代码规划和备份
- 微信小程序编辑头像
- 安全第四次课后练习
- Redis interview question (2022)
- RN development series < 9 > --mobx (1) Introduction
猜你喜欢

BSN IPFs (interstellar file system) private network introduction, functions, architecture and characteristics, access instructions

Playwright web crawler actual battle case sharing
![Text processing tool in shell, cut [option parameter] filename Description: the default separator is the built-in variable of tab, awk [option parameter] '/pattern1/{action1}filename and awk](/img/ed/941276a15d1c4ab67d397fb3286022.png)
Text processing tool in shell, cut [option parameter] filename Description: the default separator is the built-in variable of tab, awk [option parameter] '/pattern1/{action1}filename and awk

Bo Yun container cloud and Devops platform won the trusted cloud "technology best practice Award"
![[C language] recursively explain the tower of Hanoi problem](/img/a6/bbf1f19fc2a663df155cca53f2538a.png)
[C language] recursively explain the tower of Hanoi problem

ceph操作

Dry goods | how can independent station operation improve online chat customer service?

redux三大核心

从零开始C语言精讲篇4:数组

F - Pre-order and In-order(Atcoder 255)
随机推荐
Wechat applet editor Avatar
RN development series < 9 > --mobx (1) Introduction
What is the difference between using varchar type and using date type for timestamp column?
Sed output specified line
VSCode开启Pull Request更新代码分支可视化新篇章
Ref Hook
Do you know about wechat merchant billing?
There are two solutions for the feign call header of microservices to be discarded (with source code)
地平线 旭日X3 PI (四) 板上运行(未写完)
State Hook
els 方块显示原理
How can I index the Oracle database field date?
Shell programming enhancements
Use unity to build a WordArt system
The price reduction of iphone13 is just a show. Consumers are waiting for iphone14
数组中的最大值,最小值,冒泡排序
Solution to the third game of 2022 Hangzhou Electric Multi school league
Wechat input component adds a clear icon, and clicking clear does not take effect
有趣的C语言
5. Display of component dynamic components