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%por%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

  1. Install Visual Studio
  2. Install the Visual Studio plug-in, Visual Assist
  3. Visual Studio 2008 uninstall
  4. Visual Studio 2003/2015 Uninstall
  5. C format controller/placeholder
  6. C language logic operator
  7. C language ternary operator
  8. C language comma expression
  9. C sizeof and strlen are different
  10. C language strcpy and strcpy_s function difference
  11. C memcpy is different from memcpy_S
  12. C language array definition and use
  13. C array traversal
  14. C language array sort – bubble sort
  15. C language array sort – selection sort
  16. C language array sort – insertion sort
  17. C language array sort – fast sort method
  18. C array subscript is out of bounds
  19. C array memory overflow
  20. C array subscript out of bounds and memory overflow difference
  21. C language two-dimensional array definition and use
  22. C language two-dimensional array row number and column number calculation
  23. C language pointer declaration and definition
  24. P ++ / p –

C language pointer p++ / p —

This article is published by the blog – Ape Say Programming Ape Say programming!