A brief 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 allows storage of 10 strings

Every single 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. A two-D array can be changed after assignment, but the pointer array cannot be changed after it has been assigned a value.

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

3. Once the length of the array is defined, the allocated memory is also determined not to be modified. If a string is too small to be assigned to an array, it will waste space, and if it is too large, the string will not fit, but the pointer will not. Such as:


two. Array pointer: The essence is a pointer to an array of specified lengths.

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.

For example:

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 (parameter)

{

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

}

For this function, be sure to use caution, for example, to give the code of a colleague before the error occurred:

int *func () {

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

return arr;


}//This is a very dangerous practice, where the function is opened up inside the stack, the function ends after the memory is retracted. It is easy to make a bug. Unless you manually open up memory, or developers are more in-depth about the current memory distribution.

Four. Function pointer: is essentially a pointer. A pointer to a function, which 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 that has two parameters, an int that returns a value: int strcmp (char *, char *). The function pointer that defines it: Int (*p) (char*, char *), just remove the function name and change it (*P).

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

A brief 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.