当前位置:网站首页>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 !!
边栏推荐
- 结构型模式-桥接模式
- Pinia uses plug-ins for persistent storage.
- The project parameters are made into configurable items, and the @configurationproperties annotation is used
- The price reduction of iphone13 is just a show. Consumers are waiting for iphone14
- Pinia入门到精通,Pinia使用全流程,包含state,actions,getters,以及如何解构,进行响应,actions使用的多种方法
- Final review of management information system
- shell编程增强
- There are two solutions for the feign call header of microservices to be discarded (with source code)
- [day02] Introduction to data type conversion, operators and methods
- e. The difference between target and e.currenttarget
猜你喜欢

From scratch, C language intensive Lecture 4: array

Vscode opens a new chapter in the visualization of pull request update code branches
![Shell的正则表达式入门、常规匹配、特殊字符:^、$、.、*、字符区间(中括号):[ ]、特殊字符:\、匹配手机号](/img/31/ed0d8c1a5327059f2de7493bec1c6c.png)
Shell的正则表达式入门、常规匹配、特殊字符:^、$、.、*、字符区间(中括号):[ ]、特殊字符:\、匹配手机号

JS three methods of traversing arrays: map, foreach, filter

Database leader Wang Shan: strive for innovation and carefully Polish high-quality database products

Plato farm has a new way of playing, and the arbitrage eplato has secured super high returns

结构型模式-外观(Facade)模式

Structural mode - facade mode

详解左值、右值、左值引用以及右值引用

Overview of communication protocols
随机推荐
Using JSON type to realize array function in MySQL
Final review of management information system
博云容器云、DevOps 平台斩获可信云“技术最佳实践奖”
Dino paper accuracy, and analyze the variant of its model structure & Detr
grid布局
Playwright web crawler actual battle case sharing
Shell programming enhancements
Effect Hook
timestamp列使用varchar类型和使用date类型有什么区别?
Huawei's entry into the commercial market: due to the trend, there are many challenges
sram、dram、sdram、ddr的区别和用途
Nacos startup and login
Chapter 4 scope and life cycle of bean object
Head detached from origin/... Causes push failure
哈希表刷题(下)
The data in echart histogram is displayed at the top of the chart
Interview must ask | what stages does a thread go through from creation to extinction?
Prometheus node exporter common monitoring indicators
使用Unity做一个艺术字系统
Influxdb basic understanding