Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - C language: briefly describe the relationship between array and pointer?
C language: briefly describe the relationship between array and pointer?
I. Concept

Array: An array is a collection used to store multiple data of the same type.

Pointer: A pointer is equivalent to a variable, but different variables are different. It stores the addresses of other variables in memory.

Second, assignment, storage mode, sizeof, initialization, etc.

1. Assignment

Pointer variables of the same type can assign values to each other, but arrays cannot. Can only be assigned or copied one by one.

2. Storage mode

Array: Arrays are continuously stored in memory, creating a continuous memory space. Arrays are accessed according to the context of arrays, and multidimensional arrays are stored in memory according to one-dimensional arrays, but logically they are multidimensional.

The storage space of the array is either in the static area or on the stack.

Pointer: Pointer is flexible, it can point to any type of data. The type of pointer indicates the memory in the address space it points to.

Pointer: Because the pointer itself is a variable, it also stores a variable, so the storage space of the pointer cannot be determined.

Find sizeof

Array:

Memory occupied by array: sizeof (array name)

Sizeof array: sizeof (array name) /sizeof (data type)

Pointer:

On 32-bit platforms, the pointer size is 4, while on 64-bit platforms, the pointer size is 8.

I wrote in my previous blog about finding sizeof with pointers and arrays, and now I will post the connection:

/cherry dreams over/ articles/details /8 1589838

initialization

Array:

( 1)char a[]= { " Hello " }; //Initialized by a string with a size of 6. (2)char b[]={'H ',' e ',' l ',' l ' }; //Initialize by character (error, output will be garbled without terminator) (3) char c [] = {'h',' e',' l',' l',' \ 0'}; //Initialize 1234 by character.

A misunderstanding is added here, which is about the creation and destruction of arrays, especially the creation and destruction of multidimensional arrays.

(1) One-dimensional array:

int * arr = new int[n]; //Create a one-dimensional array

Delete [] arr; //destroy

(2) Two-dimensional array:

int * * arr = new int *[row]; //This is equivalent to creating an array with many rows.

for(int I = 0; I< platoon; i++)

{

arr[I]= new int[col]; //This is creation.

}

//release

for(int I = 0; I< platoon; i++)

{

Delete [] arr [I];

}

Delete [] arr;

Pointer:

//( 1) Pointer to the object: (the value in () is the initialization value) int * p = newint (0); Delete p; //(2) Pointer to the array: (n represents the size of the array, and the value does not need to be determined at compile time, but can be determined at run time) int * p = new int [n]; Delete [] p; //(3) Pointer to the class: (If the constructor has parameters, there are parameters after new Class; Otherwise, call the default constructor, and delete calls the destructor) Class * p = new Class? Delete p; //(4) pointer of pointer: (secondary pointer) int * * PP = new (int *) [1];

PP[0]= new int[6]; Delete [] PP [0]; 123456789 10

Here we distinguish two important concepts: pointer array and array pointer.

(1) Pointer array: It is actually an array, and each element of the array stores an element of pointer type.

int * arr[8]; //Priority problem: [] Priority higher than *//means that arr is an array and int* is the content of the array//This sentence means that arr is an array and int* 1234.

Please click to enter a picture description.

(2) Array pointer: actually a pointer to an array.

int(* arr)[8]; //Because [] has higher priority than *, you must enclose *arr in parentheses when writing array pointers. //arr is combined with * first, indicating that p is a pointer variable. //This sentence means that the pointer arr points to an array with the size of 8 integers. 1234

Please click to enter a picture description.

Third, inheritance and reference.

Array:

Arrays will degenerate into pointers when passing parameters, so let's first look at what degeneration is!

The meaning of (1) degeneration: C language only passes parameters in the form of value copies. When passing parameters, if only the whole array is copied, the efficiency will be greatly reduced, and when the parameters are on the stack, too much array copying will lead to stack overflow.

(2) So the C language degenerates the transfer parameters of the array. When the whole array is copied into the function, the array name is regarded as const pointer, and the address of the first element of the array is passed.

1. Pass the parameters of a one-dimensional array.

# include & ltstdio.h & gt// Pass parameters correctly//Pass parameters in the form of an array, and you don't need to specify the size of the parameters, because when passing parameters in a one-dimensional array, the formal parameters don't actually create the array, but only the address of the first element of the array. (If the variable value is passed, then the formal parameter is a copy of the actual parameter) void test(int arr[])

{}//The way to pass parameters is correct.//No parameters can be passed. Of course, parameters can also be passed as void test(int arr[ 10]).

The {}//parameter is passed in the correct way. //One-dimensional array passed the address void test (int *arr) of the first element of the array because the transfer parameter degenerated and was received by a pointer.

{}//The parameter is passed correctly /* ARR [20] is an array of pointers, and the array name void test2(int *arr[20]) is passed.

The {}//parameter is passed correctly.//is the array name of the pointer array, representing the address of the first element. The first element is a pointer to the array, and then the address is taken, which is the secondary pointer, and the secondary pointer receives void test2(int **arr).

{}int main()

{ int arr[ 10]= { 0 }; int * arr 2[20]= { 0 };

Test (arr);

test 2(arr 2);

}

2. Pass the parameters of a two-dimensional array

//Pass parameters correctly//Indicates the size of a two-dimensional array, with three rows and five columns of void test(int arr[3][5]).

{}//Incorrect parameter passed//Two square brackets of a two-dimensional array cannot both be empty, and the second one cannot be empty, only the first one can be empty void test(int arr[][]).

{}//The parameter is passed correctly//It can be written like this, but it cannot be written as intarr [3] [] void test (intarr [] [5]).

{}//Incorrect parameter passing //arr is a first-level pointer that can be passed to a two-dimensional array, but void test(int *arr) cannot be read correctly.

{}//Incorrect parameter passing//The parameters here are pointer arrays, which are one-dimensional and can be passed, but the data read is incorrect void test(int* arr[5]).

{}//Pass parameters correctly//Pass the array name of a two-dimensional array, that is, the address of the first element of the array, that is, the address of the first row, which is also an array, and receive void test(int (*arr)[5]) with an array pointer.

{}//The parameter is not passed correctly//You can pass the parameter, but there will be some problems when reading void test(int **arr).

{}int main()

{ int arr[3][5]= { 0 };

Test (arr);

}

Pointer:

1. First-level pointer passing parameter

What parameters can be accepted when the function parameter part is a first-order pointer? For example, test(int*p).

(1) can be an integer pointer.

(2) It can be an integer variable address.

(3) It can be the array name of a one-dimensional integer array.

# include & ltstdio.h & gtvoid print(int *p,int sz)

{ int I = 0; for(I = 0; I & ltSZ;; i++)

{ printf(" % d \ n " ,*(p+I));

}

}int main()

{int arr[ 10] = { 1,2,3,4,5,6,7,8,9 }; int * p = arrint SZ = sizeof(arr)/sizeof(arr[0]); //the first-level pointer p is passed to the function print(p, SZ); Returns 0;

}

2. Secondary pointer transmission parameters

That is, what parameters can be accepted when the function parameter part is a secondary pointer, such as: test(int**p).

(1) auxiliary pointer variable

(2) The address of the first pointer variable

(3) the array name of one-dimensional pointer array

# include & ltstdio.h & gt Invalid test (internal ** ptr)

{ printf(" num = % d \ n " ,* * ptr);

}int main()

{ int num = 10; int * p = & ampnumint * * pp = & ampp;

Test (PP);

Test (& AMPP); Returns 0;

}

Four, function pointer, function pointer array, function pointer array pointer

1. function pointer

Invalid test ()

{printf ("Hehe \ n");

}//pfun can store the address void(* pfun)(); () of the test function;

Form of function pointer: type (*), for example: int (*p) (). It can store the address of a function, which is also common in common applications.

2. Function pointer array

Form: such as int (* p [10]) ();

Because p is combined with [] first, it means that p is an array, and the content of the array is an int (*) () pointer.

Function pointer arrays are widely used in conversion tables.

3. Pointers of function pointer array

A pointer to an array of function pointers, that is, an array whose elements are all function pointers.

Void test (const char* str)

{printf("%s\n ",str);

}int main()

{//function pointer pfunvoid (* pfun) (constchar *) = test; //array of function pointers pfunarrvoid (* pfunarr [5]) (constchar * str);

pfunArr[0]= test; //pointer pfunarrvoid (* (ppfunarr) [10]) (constchar *) =&; PfunArr returned 0;

}