Introduction to pointer arrays, array pointers, pointer functions, function pointers

Source: Internet
Author: User

A. Array of pointers: essentially an array in which the elements that are stored are pointers. For example: Char *nums[10]; defines a storage element named num length 10 as an array of pointer variables that point to char type data. Each element in the array is a pointer, and each pointer can point to a string.

Examples in the program are:

int main (int argc, const char * argv[]) {

A pointer array is an array used to represent a string

Char *strs[10];

Declares an array that agrees to store 10 strings

Each string

Strs[0] = "1234";

Strs[1] = "Hello";

// ...

STRS[9] = "Hello IOS";

return 0;

}

The difference between a pointer array and a two-dimensional array:

1. A two-D array assignment must be assigned using a For loop or strcpy () function, and the pointer array can be assigned directly.

Such as. Arr1[]= "QAZWSX";

2. The two-D array can be changed after assignment. However, the pointer array cannot be changed after it has been assigned.

Array pointer: Essence is a pointer. Pointer to an array of variables (the address of the array to which the variable is stored).

3. Once the length of the array is defined, the allocated memory is determined not to be altered. Assuming that the string is too small to be allocated to the array, it wastes space, assuming it is too large for the string to be filled, but the pointer does not.

For example, with:


two. Array pointer: essence is a pointer. An array that points to a specified length.

Definition: such as int (*P) [10];

int refers to the type of the element of the array to which the pointer is pointing.

(*p) refers to a pointer variable name that defines a name that is p. [10] refers to the length of the array to which the pointer is pointing. A pointer variable p is defined, which points to an array of type int with an element length of 10.

Like what:

int main (int argc, const char * argv[])

{

int (*p) [10]= "1 2 3 4 5 6 7 8 9 0";

printf ("%s", *p);

return 0;

}

Expansion: int * (*P) [10]. Refers to the definition of a pointer-type variable p, pointing to a length of 10 to hold pointer-type elements of data.

three. Pointer function: essence is a function.

The return value is a pointer.

Int *function (number of references)

{

............

}

For such a function. Must be used with caution. Give me a sample example. Give the code before a colleague who has an error:

int *func () {

Intarr[] = {1,2,3,4};

return arr;


}//This is a very critical practice, where the function of opening up exists in the stack. The memory is retracted after the function ends. Very easy to bug out. Unless you manually open up memory, or the developer is more in-depth about the current memory distribution.

Four. Function pointer: is essentially a pointer. A pointer to a function. This means that the pointer variable holds the address of the function.

Core meaning: Use a variable to store a function and then call the function with a variable

Grammar:

For example, define a function with two parameters, an int return value: int strcmp (char *, char *).

The function pointer that defines it: Int (*p) (char*, char *); only need to remove the function name (*P).

Introduction to pointer arrays, array pointers, pointer functions, function pointers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.