This is the 24th day of my participation in the August Text Challenge.More challenges in August
directory
- I ++/ I –
- P ++/p –
- 1. Access array elements by subscript
- 2. Access array elements by pointer offset
- Guess you like it
C/C++ learning Directory >> C language basics
I ++/ I –
In the previous article, the increment and subtraction operators (++ I/I ++) introduced the variable increment and subtraction operations, for example:
int x = 10; x++; // equivalent x = x + 1 = 11 ++x; // equivalent x = x + 1 = 12 --x; // equivalent x = x- 1 = 11 x--; // Equivalent x = x - 1 = 10Copy the code
before++
After /++
The difference between:
before--
After /--
The difference between:
P ++/p –
Pointer: A pointer is equivalent to a variable. It holds the memory address of the variable. It is a hexadecimal type and can point to any type of data.
** arrays: **** arrays are contiguous in memory, creating a contiguous memory space **. Arrays are accessed according to their subscripts, and multidimensional arrays are stored in memory as one-dimensional arrays, only logically multidimensional.
1. Access array elements by subscript
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: programming ape said // @blog (personal Blog address): www.codersrc.com // @file :C language tutorial - C language pointer P ++ / p - // @time :2021/06/16 08:00 // @motto: a thousand miles without a step, a small stream without a river or sea, The wonderful program life needs to accumulate unremittingly! /******************************************************************************************/ #include<stdlib.h> #include<stdio.h> void main() {int arr[5] = {10,20,30,40,50}; int len = sizeof(arr) / sizeof(arr[0]); For (int I = 0; i<len; i++) { printf("arr[%d] = %d\n", i, arr[i]); } system("pause"); } /* Output: ARr [0] = 10 ARr [1] = 20 ARr [2] = 30 ARr [3] = 40 ARr [4] = 50 Press any key to continue... */Copy the code
2. Access array elements by pointer offset
A pointer, as a variable, must have its own address,A placeholderuse%p
or%x
;
int *p = 10; printf(" p : %p",p); // Output address :004FF798Copy the code
** as a variable, it must have its own value. The placeholder is %d, and the value of the pointer must be preceded by *. Otherwise, it is the address of the pointer.
int *p = 10; printf(" p : %d",*p); Printf (" p: %p",p); // Output address :004FF798Copy the code
Arrays are contiguous in memory, so arrays can be accessed by pointer offsets, for example:
Int arr[5] = {10,20,30,40,50}; int *p = arr; // equivalent *p = arr[0]; int value1 = *p++; // int value1 = arr[1]; int value2 = *p++; // int value2 = arr[2]; int value3 = *p++; // int value3 = arr[3]; int value4 = *p++; // int value4 = arr[4];Copy the code
In the above example, we use p to point to the array arr. By default, p points to the first element arr[0]. We use the *p++ offset to get the value of each element in the array.
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: programming ape said // @blog (personal Blog address): www.codersrc.com // @file :C language tutorial - C language pointer P ++ / p - // @time :2021/06/16 08:00 // @motto: a thousand miles without a step, a small stream without a river or sea, The wonderful program life needs to accumulate unremittingly! /******************************************************************************************/ #include<stdlib.h> #include<stdio.h> void main() {int arr[5] = {10,20,30,40,50}; int* p = arr; Int len = sizeof(arr)/sizeof(arr[0]); For (int I = 0; i < len; //&arr[0]: arr[0]: arr[0]: arr[0]: arr[0]: arr[0]: arr[0]: arr[0]: arr[0]; } printf("\n"); for (int i = 0; i<len; I ++) {// p: is an address //*p: takes the value of the corresponding address of p printf(" loop by pointer offset: array subscript: %d: the corresponding element address is: %d\n", I, p, *p); p++; // address offset +1, equivalent offset to the next element address} system("pause"); } /* Output: loop according to the index value: array subscript: 0 the corresponding element address is: 561904944 the corresponding element value is: 10 Loop according to the index value: array subscript: 1 the corresponding element address is: 561904948 the corresponding element value is: 20 Loop according to the index value: Array subscript: 2 The corresponding element address is: 561904952 the corresponding element value is: 30 Loop by index value: array subscript: 3 The corresponding element address is: 561904956 The corresponding element value is: 40 Loop by index value: array subscript: 4 The corresponding element address is: 561904956 561904960 The value of the element is: 50 Offset by pointer: array subscript: 0 The value of the element is: 561904944 10 Offset by pointer: array subscript: 1 The value of the element is: 561904948 20 Offset by pointer: array subscript: 2 The corresponding element address is 561904952 the corresponding element value is 30 Offset by pointer: array subscript: 3 The corresponding element address is 561904956 the corresponding element value is 40 Offset by pointer: array subscript: The address of the element corresponding to 4 is: 561904960 The value of the element corresponding to 50 Press any key to continue.. */Copy the code
Guess you like it
- Install Visual Studio
- Install the Visual Studio plug-in, Visual Assist
- Visual Studio 2008 uninstall
- Visual Studio 2003/2015 Uninstall
- C format controller/placeholder
- C language logic operator
- C language ternary operator
- C language comma expression
- C sizeof and strlen are different
- C language strcpy and strcpy_s function difference
- C memcpy is different from memcpy_S
- C language array definition and use
- C array traversal
- C language array sort – bubble sort
- C language array sort – selection sort
- C language array sort – insertion sort
- C language array sort – fast sort method
- C array subscript is out of bounds
- C array memory overflow
- C array subscript out of bounds and memory overflow difference
- C language two-dimensional array definition and use
- C language two-dimensional array row number and column number calculation
- C language pointer declaration and definition
- P ++ / p –
C language pointer p++ / p —
This article is published by the blog – Ape Say Programming Ape Say programming!