C language (array and pointer)

Array

  char a [n]; when n is an integer or integer expression, it is not a variable-length array, otherwise the array is variable-length; (c99)
  character arrays can be printed at once in string format.

// Assignment method: 
char a [] = " abcde " ;
 int a [ 10 ] = { 1 , 2 , 3 }; // The ones without assignment are all 0; 
int a [] = { 1 , 2 , 3 , 4 , 5 }; // Automatically calculate the length 
int a [ 10 ] = {[ 3 ] = 3 , [ 5 ] = 5 , [ 8 ] = 8 }; // Specify the assignment
 // Two-dimensional array: 
int a[2][3]={1,2,3,4};
int a[2][3]={{1,2},{3}};
int a[2][3]={[0][1]=1,[0][2]=2,[1][0]=3};
int a[][3]={1,2,3, 4 , 5 , 6 , 7 }; // You can not write the number of rows, but the number of columns must be written

pointer

  Variables store data, pointers store addresses;

  The memory occupied by the pointer is 4 bytes , unlike variables ( int (4), char (1), float (4), double (8)); [Address: & a; Value: * a;]

int a = 1 ; int * b = & a ;
 // If the assignment is not an address, the pointer points to the specified location represented by the integer data
 // The placeholder for the address is% p

Numeric name

  It is an address information (the array name is a pointer constant ), and it is also the address of the first element of the array; so [array / pointer] can also access the elements using [pointer / array].

  Arrays can only access each element separately (each element is an lvalue) , not an entire array ; (function names have similar properties)

// The difference between the pointer and the array name: the pointer is an lvalue (variable, used to store the address), and the array name is an address constant (rvalue); 
int a, b, c = 0 ;
 int * p [ 3 ] = { " asdf " , " qwer " , " czxnh " }; // Pointer array: an array storing only pointers; 
int (* p) [ 3 ]; // Array pointer: pointer to the array (the unit of pointer movement) Is the size of the array)
 // When assigning a value, assign him the address of the array: int (* p) [3] = & one-dimensional array name

Array pointers and two-dimensional arrays

  Two-dimensional array

  Each element is a one-dimensional array; so the two-dimensional array name is the address of the first one-bit array;

int array [ 4 ] [ 5 ]; array == array [ 0 ];
 * (array + 1 ) == array [ 1 ] == & array [ 1 ] [ 0 ];
 // All are addresses, add a * in front The value at this location can be retrieved
 // ie: * (* (p + i) + j) == p [i] [j];

  Array pointer

  int (*p)[5]=array;

  Pointer to array

  The unit of array pointer movement is the array size : * (* (p + i) + j) == p [i] [j];


void (untyped) pointer and NULL (kong) pointer
   void* p:untyped pointer, whichcan point to any type of data; forced type conversion is required when dereferencing;* (int*) p;
  NULL pointer: does not point to any data: [In fact, NULL is a macro definition: 0; #define NULL ((void*) 0) 0 address is generally not pointed];
    when the pointer is defined and the address is not known, you can point to NULL [int* p = NULL; 】


Secondary pointer

  Store the address of the pointer; [One-level dereference is only the address of one pointer ]
  Because the second-level pointer points to the pointer, the unit of the second-level pointer movement is the pointer size (4 bytes)

int ** p1, (* p2) [ 4 ]; // p1 is the second-level pointer, p2 is the array pointer; 
* (p1 + 1 )-* p1 = 4 ; // Because the second-level pointer points to the int pointer, the span is 4 
* (p2 + 1 )-* p2 = 16 ; // Because this array pointer points to an int array of length 4, the span is 16

Note: As mentioned above, when an array pointer points to an array, there is * (* (p2 + i) + j) == array [i] [j]; the second-level pointer does not have this property.


Constants and pointers

  1. Macro definition: define A 'a'
  2. Use  const  keyword (read-only not write): const float pai = 3.14;

Pointer to constant

const  int * p;
 // Pointer to constant, point to constant. The value pointed to by address can not be modified by dereference; the value dereferenced can be modified indirectly by modifying to a different constant 
int * const p;
 // Point to a constant pointer, point to a variable. Can not change the value of the address pointed to by dereferencing; you can modify the value of the variable to indirectly modify the dereferenced value 
const  int * const p;
 // A constant pointer to a constant; [A pointer to a constant can also point to a variable]

Guess you like

Origin www.cnblogs.com/TianLiang-2000/p/12713005.html