C pointers notes

pointer


Pointer types

XX Type + * → XX pointer type

int *   shaping pointer type

char *  char pointer type

 

Pointer variable

Variable pointer is a pointer variable type declaration

int a; // declares an integer variable

int * a; // declare a pointer to an integer variable

 

Pointer variable assignment

Pointer variable is stored in the memory address of a variable assignment principle: What type of pointer variable, it should be given the address of the variable value of the corresponding type:

① int * p = 0x40003a04; // easy interpretation

② int  a ;  char b = ‘B’ ;  int *p1 = &a ;  char *p2 = &b ;

 

Memory address

Memory means RAM in a particular location, usually expressed in hexadecimal numbers, such as 0x40003a04;

In the 32-bit cpu , the address is a 32 -bit unsigned integer number, may represent 2 ^ 32 (4GB) address.

 

Nature pointer

Regardless of the type of pointer, the address must be stored, pointing to the address of the corresponding variable, and the address value is a 32 -bit unsigned integer, any type of pointer size is 4 bytes;

Pointer type → unsigned int

 

Common pointer type

Types of

int

char

float

double

struct A{int a;int b;}

void func()

Pointer types

int*

char*

float*

double*

struct A*

typedef void(*pFunc)()

 

Define and initialize the pointer

int* p1 = NULL ;

char* p1 = NULL ;

float* p1 = NULL ;

double* p1 = NULL ;

 

Pointer variable assignment

int a = 5 ;  int* p1 = &a ;

 

Get the value of the pointer variable referred variable

Use * Get the value of the pointer variable

printf ( “%d” , *p1) ;

 

* The two uses

Type *

Pointer types

* Pointer variable

Fetching pointer indicated value of the variable

 

Pointer type structure type

Types of

struct A{int a;int b;}

Pointer types

struct A*

Pointer variable definition of the structure

struct A* p = NULL ;

Obtaining the address of the structure variable

struct  A  {int a;int b;};

struct  A  a ;

printf ( “%p” , &a) ;

 

Use * Get structure pointer variable value

struct  A  { int a ; int b ; } ;

struct  A  a = { 5 , 6 } ;

struct  A* p = &a ;

printf ( “%d , %d” , (*p).a , (*p).b ) ;

Using the pointer variable access structure members

By -> reference to a structure member variables

struct  A  { int a ; int b ; } ;

struct  A  a = { 5 , 6 } ;

struct  A* p = &a ;

printf ( “%d , %d” , p->a , p->b ) ;

 

 

Function pointer: Pointer symbol * in front of the function name

Function: void FUNC () {the printf ( "FUNC \ n-");}

 

Address of the function

For the function name, which is the first address: the same address to take the following two:

printf ( “%p\n” , func );

printf ( “%p\n” , &func);

 

Function pointer type , without typedef represents defines a function pointer variable

Function prototype

void func()

Pointer types

typedef void(*pFunc)()

 

Function pointer assignment

pFunc p = func; or pFunc p = & func;

Call the function using the function pointer variables

pFunc  p = func ;

p (); // calls func function

 

Complex function pointer

int ( * (* p )( int ) )( int );

eg. typedef int( * (* pFunc3 )( int ) )( int );

pFunc3  func3 = func;

 

Wild pointer

A pointer memory area illegal

points to memory has been destroyed

point does not have access memory

①int* p = (int* )malloc( int ) ;

free (p); // this time p when the field has become a pointer, then take the value of the address, distortion

randomly p points to a memory area of uncertainty unknown; would cause a memory to be modified, causing subtle BUG or system crashes

 

Avoid dangling pointers

pointer initialize the application: int * P = NULL ;

released after the memory pointer is set to NULL : Free (P); P = NULL;

 

Pointer arithmetic: addition / subtraction

Pointer addition: Pointer + N : shows a pointer value plus N * sizeof ( type ) bytes

you you you you

↑     ↑

int*p   p+3

Pointer addition: ++ pointer: shows a pointer value plus sizeof ( type ) bytes, after running the pointer p value changes

you you you you

 ↑   ↑

int*p ++p

Pointer subtraction method: pointer -N : shows a pointer value minus the N * sizeof ( type ) bytes

you you you you

↑     ↑

p-3     int*p

Pointer subtraction method: - Pointer: the pointer indicates the value obtained by subtracting sizeof ( type ) bytes, after running the pointer p value changes

you you you you

↑     ↑

--p int*p

 

Pointer - Pointer: indicates the number of two bytes obtained by subtracting the address value: P2 = P1. 3-

you you you you

↑  ↑

int*p1 int*p2

 

One-dimensional array

int a [5] = {1,2,3,4,5 }; occupies a contiguous memory space, each value in the array of one byte;

First address one-dimensional array is an array of name or address of the first element;

int a[5] = {1,2,3,4,5};

int*p = a ;

int*p = &a[0] ;

 

The difference between the pointer and array name

Types of

Whether variable

Computable

Array name

Immutable, it is a constant

Unable ++ , -

pointer

Can change, it is variable

May ++ , -

 

Use pointers to access array elements

for( int i = 0 ; i < 5 ; i++ )

{

printf(“%d\n” , *(p+i));

}

 

for( int i = 0 ; i < 5 ; i++ )

{

printf(“%d\n” , *(p++));

}

 

for( int i = 0 ; i < 5 ; i++ )

{

printf ( "% d \ n" , * (a + i)); // array name is a constant, not changing, by the addition of i offset value, not from the subtraction

}

 

Two-dimensional array

int a[2][3] = {{1,2,3},{4,5,6}};

a[0]1 2 3

a[1]4 5 6

First address two-dimensional array is an array of name or address of the first element;

 

Defined two-dimensional array pointer

Point two-dimensional array of pointers: row pointer → type (* p) [N]

int a[2][3] = {{1,2,3},{4,5,6}};

int (*p)[3] ;

p = a; // assign an array name

p = & a [0]; // pointer to the first address of the first element

 

Use pointers to access two-dimensional array of elements

for( int i = 0 ; i < 2 ; i++ )

for( int j = 0 ; j < 3 ; j++ )

{  printf(“%d\n” , p[i][j] );  }

 

for( int i = 0 ; i < 2 ; i++ )

for( int j = 0 ; j < 3 ; j++ )

{Printf ( "% d \ n ", * (p [i] + j));} // p [i] representative of the row pointer, the p [i] seen as an array name

 

for( int i = 0 ; i < 2 ; i++ )

for( int j = 0 ; j < 3 ; j++ )

{  printf( “%d\n” , *(*(p+i)+j) );  }

 

a [m] [n] is equivalent wording

Types of

Pointer definitions

Assignment

Row pointer

int(*p)[N]

a      &a[N]

Ordinary pointers

int*

a[N]  &a[M][N]

 

String constants is itself an address

hello stored in memory in hello \ 0

String constants essence is a section of memory, the first address to identify, and the value of the string constants can not be changed

 

String pointer

char * p = "hello"; not use P [. 1] = 'A'; change the contents of

Imparting to the pointer variable string constants first address;

for( int i = 0 ; i < 5 ; i++ )

{ printf( “%c\n” , p[i] );  }

 

for( int i = 0 ; i < 5 ; i++ )

{ printf( “%c\n” , *(p+i) );  }

 

for( int i = 0 ; i < 5 ; i++ )

{ printf( “%c\n” , *(p++) );  }

 

Pointer Pointer

Pointer variables take up memory, there is also a memory address

Pointer type:

XX Type    * XX pointer type

XX Type *    XX pointer type   // pointer type

int ** // pointer of type int pointers

char ** // character shaped pointer pointer type

float ** // floating point types pointer to a pointer type

eg.

int a = 10;

int* p = &a;

int** p = &p;

 

The use of void -parameters

void * can pass any type of address

void func(void *p)

{// the p converted to the actual type }

 

Cast pointer variable

int a =5 ;

int *p = &a;

int c = (int)p;

int *p2 = (int*) c;

 

Cast pointer variable

char buf[256]

int* p = (int*) buf ;

*p = 123;

printf( “%d\n” , *p);

 

char buf[256]

struct A{int a;int b;};

A* p = (A*) buf ;

p->a = 12;

p->b = 34;

printf( “%d,%d\n” , p->a , p->b);

 

Guess you like

Origin www.cnblogs.com/ren-hang/p/12644182.html