当前位置:网站首页>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 !!
边栏推荐
- 在有序数组找具体某个数字
- Interview must ask | what stages does a thread go through from creation to extinction?
- Scala immutable map, variable map, map conversion to other data types
- Effect Hook
- IIC communication protocol (I)
- From scratch, C language intensive Lecture 4: array
- Use the kubesphere graphical interface dashboard to enable the Devops function
- Wechat input component adds a clear icon, and clicking clear does not take effect
- els_ 画矩形、代码规划和备份
- Understand kingbasees V9 in one picture
猜你喜欢

Dino paper accuracy, and analyze the variant of its model structure & Detr

干货 | 独立站运营怎么提高在线聊天客户服务?

IIC communication protocol (I)

Visualization domain svg

数组中的最大值,最小值,冒泡排序

Safety fourth after class exercise
![Introduction to regular expressions of shell, general matching, special characters: ^, $,., * Character range (brackets): [], special characters: \, matching mobile phone number](/img/31/ed0d8c1a5327059f2de7493bec1c6c.png)
Introduction to regular expressions of shell, general matching, special characters: ^, $,., * Character range (brackets): [], special characters: \, matching mobile phone number

Use unity to build a WordArt system

There are two solutions for the feign call header of microservices to be discarded (with source code)

Final review of management information system
随机推荐
2022杭电多校联赛第三场 题解
VSCode开启Pull Request更新代码分支可视化新篇章
第4章 Bean对象的作用域以及生命周期
【动态规划百题强化计划】11~20(持续更新中)
BSN IPFs (interstellar file system) private network introduction, functions, architecture and characteristics, access instructions
Shel automatically sets directory permissions
Anonymous named pipes, understanding and use of interprocess communication in shared memory
RN开发系列<9>--Mobx(1)入门篇
【C语言】递归详解汉诺塔问题
【AtCoder Beginner Contest 260 (A·B·C)】
第六章:云数据库
0 dynamic programming medium leetcode467. The only substring in the surrounding string
ELS compatibility DC, transfer pictures to window
The data in echart histogram is displayed at the top of the chart
Ref Hook
Wechat input component adds a clear icon, and clicking clear does not take effect
F - Pre-order and In-order(Atcoder 255)
Structural mode - decorator mode
5. Display of component dynamic components
Easy to use mobile app automation testing framework where to find? Just collect this list!