当前位置:网站首页>Hello C (V) -- pointer and array
Hello C (V) -- pointer and array
2022-06-24 23:42:00 【Tianshan old demon】
An array is C Language built-in data structure , A thorough understanding of arrays and their usage is the basis for developing efficient applications . Arrays and pointers are closely related , But it is not completely interchangeable .
One 、 Introduction of the array
An array is a continuous collection of elements of the same type that can be accessed by index . The elements of an array are contiguous in memory , There is no gap in the middle , The elements of the array are of the same type .
1、 Array interpretation
Definition of array :int a[10] = {0,1,2,3,4,5};
a[0]: The first element of the array , First element ( When the left value is used, it means the 0 Memory space of elements )
&a: Address of array , Is a constant , You can't be left-handed , Type equivalence int (*)[10]( Array pointer ).
&a[0]: Array number 0 Addresses of elements , And array name a Equivalent
a:a It's an array name , You can't be left-handed , The right value represents the address of the first element of the array , And &a[0] identical .
The address of an array is not the same as the address of the first element of an array .
Array names can be thought of as const The pointer , But the array name is sizeof The parameters of the operator and & Except for parameters of the operator , Array name is not a pointer .
An array is essentially a contiguous memory space .
2、 One dimensional array
One dimensional arrays are linear structures , Access members with an index .
int vector[5] = {1,2,3,4,5};
The internal representation of an array does not contain information about the number of its elements , The array name simply refers to a piece of memory .
3、 Two dimensional array
Two dimensional arrays use rows and columns to identify array elements , A two-dimensional array can be regarded as an array of arrays .
int vetor[2][3] = {
{1,2,3},{4,5,6}};The array name of a two-dimensional array is equivalent to the array pointer int (*vector)[3];
Two 、 Array and pointer representations
1、 How to access array elements
A、 Array subscript access
Array name [ Index subscript ];
a[i] <==>i[a]
B、 Pointer access
*( The pointer + Offset );
*(a + 2);// Equivalent to a[2]
The address of the element in the array is continuous .
C、 Pointer operation of array name
a + n ==> p + n*sizeof(*a)
&a + 1 ==> &a + sizeof(a)
C The language does not enforce the bounds of arrays , Therefore, accessing an array with an invalid index will cause the array access to be out of bounds , Cause unexpected behavior .
2、 The difference between array and pointer
int vector[5] = {1,2,3,4,5};
int *p = vector;vector[i] The way the array elements are accessed represents the slave address vector Start , Move i Take out the contents in places
*(vetcor+i) Accessing array elements means that from vector Start , Add... To the address i, Take out the contents of the address .
sizeof(vector) The result is the number of bytes allocated by the array
sizeof(p) What you get is a pointer variable p The length of
3、 ... and 、 Arrays as function arguments
1、 Pass a one-dimensional array
Passing a one-dimensional array as a parameter to a function actually passes the address of the array by value , There is no need to pass the entire array , There is no need to allocate space on the stack , Usually you also need to pass the array length , Make sure that the access to the array does not cross the bounds .
There are two ways to declare an array in a function declaration :
A、 Array representation
void display(int a[], int size);By using sizeof(a) It is wrong to count the number of elements of an array ,sizeof(a)/sizeof(int) That's right. .
B、 Pointer representation
void display(int *a, int size);When an array is a function parameter , Arrays degenerate into pointers .
2、 Pass multidimensional array
When passing a multidimensional array , You need to decide whether to use array representation or pointer representation in the function prototype declaration , And the dimension of the array and the size of each dimension . To use array notation inside a function , The form of the array must be specified , Otherwise the compiler will not be able to use subscripts .
void display(int a[][5], int rows);
void display(int (*a)[5], int rows);
void display(int *a[5], int rows);// Error declaration , Grammar is right , But the compiler will think that the array passed in by the function has 5 An integer pointer .When passing an array of more than two dimensions , In addition to the first dimension , You need to specify the length of other dimensions .
When a two-dimensional array is used as a function parameter , A two-dimensional array degenerates into an array pointer .
When a pointer array is used as a function parameter , Pointer arrays degenerate into two-dimensional pointers .
Four 、 Array pointer
1、 An array type
C Arrays in the language have their own specific types , The type of array is determined by the number of array elements and the size of the array .
int array[n] The array type of is int[n];
C Language can be used to typedef Rename the array type .
typedef type (name)[size];name Is an array type
The code example is as follows :
#include <stdio.h>
typedef int (array)[5];
int main(int argc, char *argv[])
{
array a;
int i;
for(i = 0; i < sizeof(a) / sizeof(int); i++)
{
a[i] = i;
printf("%d\n", a[i]);
}
return 0;
}2、 Array pointer
An array pointer is a pointer to an array .
int (*p)[5];// Array pointer
Array pointers are pointer variables that point to arrays , The array pointer holds the address of the array , Equivalent to a secondary pointer .
int a[5];
p = &a;//&a Is the address of the array , Same as array pointer type
Array pointers can be defined by array types , It can also be defined directly .
Pointer definition of array type :
typedef int (array)[5];
array *a;
Direct definition of array pointer :
type (*array)[n];
Definition of array pointer type :
typedef type (*ArrayPointer)[n];
Operation of array pointer :
array + 1 ===> array + sizeof(*array) ==>array + sizeof(int[5])
Code instance :
#include <stdio.h>
typedef int (*ArrayPointer)[5];
int main(int argc, char *argv[])
{
ArrayPointer pa;
int (*p)[5];
int a[5] = {1,2,3,4,5};
pa = &a;
p = &a;
int i;
for(i = 0; i < sizeof(a) / sizeof(int); i++)
{
printf("%d\n", a[i]);
(*pa)[i] = 10;
printf("%d\n", a[i]);
(*p)[i] = i+1;
printf("%d\n", a[i]);
}
return 0;
}3、 Pointer array
A pointer array is an array whose element type is a pointer , The definition of pointer array is as follows :
type * array[n];
Array array There is n Elements , Each element store type * The pointer .
int *vector[5];// Pointer array , The elements in the array are pointer variables
Code instance :
#include <stdio.h>
int main(int argc, char *argv[])
{
const char *keyword[] =
{
"do",
"for",
"return",
"while",
NULL
};
unsigned int i = 0;
while(keyword[i] != NULL)
{
printf("%s\n", keyword[i++]);
}
return 0;
}The memory allocation of pointer array and array pointer is shown in the following figure :

边栏推荐
- 7-6 laying oil well pipeline
- Stm32f030f4 reading infrared remote control data
- Simple use of libnum Library (hexadecimal string conversion)
- QT display RGB data
- 2021-2022 China's financial digitalization "new" insight Industry Research Report
- Actipro WPF Controls 2022.1.2
- 7-2 求解买股票问题
- 7-2 后序+中序序列构造二叉树
- Morris遍历
- 7-3 maximum sub segment and
猜你喜欢

无鸟用的SAP PA证书,刚入行的同行可以考一考

SAP PA certificate for no birds, which can be tested by new peers

Continuous soul torture from two MySQL indexes of interviewers

Installing IBM CPLEX academic edition | CONDA installing CPLEX

Today's sleep quality record 79 points

Fibonacci
![[JS] - [string - application] - learning notes](/img/dc/f35979b094f04c0ee13b3354c7741d.png)
[JS] - [string - application] - learning notes
![[JS] - [array application] - learning notes](/img/8a/808fde0cc86e0ec5e1f5558ba196b4.png)
[JS] - [array application] - learning notes

Idea creation module prompt already exists

Quickly build KVM virtual machine on # yyds dry goods inventory # physical machine
随机推荐
Why is it that the "Zhongtai" that was originally eaten by civil engineering is no longer fragrant?
QT display RGB data
Nominal resistance table of patch resistors with 5% and 1% accuracy
js监听页面或元素scroll事件,滚动到底部或顶部
[JS] - [array application] - learning notes
Is there really something wrong with my behavior?
华为机器学习服务语音识别功能,让应用绘“声”绘色
Start QT program
Common regular expressions
openGauss内核:简单查询的执行
First person singular reading notes
MySQL 表的增删查改
Annual salary of millions, 7 years of testing experience: stay at a fairly good track, accumulate slowly, wait for the wind to come
Printf redirection of serial port under sw4stm32 (SW4)
throttle-debounce.js:一个小型的防抖节流函数库
Pseudo original intelligent rewriting API Baidu - good collection
Volcano becomes spark default batch scheduler
R语言dplyr包select函数将dataframe数据中的指定数据列移动到dataframe数据列中的第一列(首列)
Design and practice of vivo server monitoring architecture
Tremblement de terre réel ~ projet associé unicloud