C Programming 5th-pointers and Arrays

Source: Internet
Author: User
C Programming 5th --- pointer and array

A pointer is a variable that stores the memory address of other variables; a pointer is the soul of C language!
Pointers are everywhere in C programming. A pointer is sometimes the only way to express computation. They provide a more concise and efficient method;
Pointers are the most closely related to arrays, so it is necessary to describe them with arrays;
It is easy to create a new pointer pointing to an unknown position. However, as long as there are principles, the pointer can be transparent and simple.

5.1 pointer and memory address
Pointer declaration, such as regular variables, with data types;
For example:
Int x = 1;
Int y = 2;
Int Z [10];
Int * IP;/* declares a pointer to an int. '*' is only an identifier */

IP = & X;/* the IP address now points to x */
Y = * IP;/* y the current value is 1, '*' is the resolution function */
* P = 0;/* the current value of X is 0 */
IP = z;/* IP address of the first element a [0] of the Z array */
A) '&' is the address of a variable, while '*' is the value of an address. They are a pair!
B) For arrays, the array name is the first address of the array when assigned to the pointer;
C) * IP addresses can be involved in other operations, such as ++ * IP addresses and (* P) ++ (because ++ and * are at the same level, and from right to left)
D) You can assign values between pointers. For example, if IQ = IP, IQ points to the IP address executed by the IP address;

5.2 pointer and function parameters
The parameters of Function C are passed by values;
Example: Exchange Value Function
Void swap (int x, int y)
{
Int temp;

Temp = X;
X = y;
Y = temp;
}
The above results fail to meet the expectation, because X and Y are copies of the values of real parameters. After the copy value is changed, the function is released after the function call is complete, and the value of real parameters cannot be modified;
You can change the value of the real parameter memory address:
Void swap (int * PX, int * Py)
{
Int temp;

Temp = * PX;
* PX = * py;
* Py = temp;
}
Actual call format: swap (& A, & B). The incoming address is called because PX and Py are pointers.
Note: Only by using a pointer to modify the value that points to the address can the value of the execution address be fundamentally changed.

5.3 pointers and Arrays
Array, for example, int A [100]; array name mentioned previously. It is the first address of the array when it is assigned to a pointer variable; A [0] And a [1] point to the corresponding address. Therefore, you can change the value stored in this location through a [0] = 'x;
You can declare a pointer and execute an array, such:
Int * pA;
Pa = A; or Pa = & A [0];
Then, elements can be accessed through * (PA + I;
Therefore, previous function calls such as strlen (char a []) can be changed to strlen (char * s), and S is the first address of the first element of array;
Note: A and & A have different meanings. A is the first element address of the array, and a is the first address of the entire array;

5.4 address operations
Because the pointer executes the memory address, other addresses can be accessed through the p + I method, which involves the address operation;
For example, PA + = n, a pointer to the array, can be understood as the nth element to access the array; the P + = n of the memory address P can be understood as the address after N address units;

5.5 string pointers and functions
The pointer can point to an array. If the array is a character array, the pointer to the character array is a string pointer;
For example:
Char * pmessage;
Char message [] = "Now is the time ";

Pmesloud = message;
In this case, pmessage is a string pointer. Because message is the region where the string and '\ 0' are stored, you can use * (pmessage + I) to modify an element of the message array;
Note the difference: char * pmessage = "Now is the time", the pmessage is a pointer, pointing to a String constant, cannot attempt to pass * (pmessage + I) = 'x' to modify string constants;

5.6 pointer array, array pointer, and pointer to pointer

1) pointer array, as its name implies, that is, all elements of the array are pointers; for short, "an array storing Pointers ";
For example:
Int array [10] [20];
Int * parray [10];
Parray = & array [0];
By operator priority and Value Order, [] priority and * rating. The value ranges from right to left. parray is an array consisting of 10 elements, all of which are pointers;
As shown above, each parray element points to the first address of each one-dimensional array of the array, that is, parray + 1 = & array [1]; each * (parray + I) the element points to an array of 20 elements;

Pointer array Initialization
For example:
Char * name [] = {
"Illegal month ",
"January", "February", "March ",
"Maid", "may", "June ",
"July", "August", "September ",
"October", "November", "December"
};
Function calls of pointer arrays, for example:
Void print (char * name [])
{
}
Each * (name + 1) is an array of characters. Therefore, print (name) is called );

2) array pointer. First, it is a pointer pointing to an array"
In 32, the system occupies 4 bytes;
For example, INT (* P) [10], first () has a higher priority than [], then P is first combined with * as a pointer variable; [10] is an anonymous integer array;
P points to the first address of the array;

3) pointer to the pointer, that is, the pointer variable points to the pointer
For example:
Int A = 10;
Int * P1;
Int ** P2;

P1 = &;
* P2 = p1;
P2 stores the memory address of P1, while * P2 stores the address pointed to by P1, that is, & A, and ** P2 is the value stored by access;

5.7 command line parameters
When executing a program, parameters can be attached, such:
Int main (INT argc, char * argv [])
{
Return 0;
}

5.8 pointer to function
Simple Example:
Int max (int x, int y)
{
Return (x> Y )? X: Y;
}
Int main ()
{
INT (* p) ();/* point to function pointer */

P = max;/* points to the max function */

Printf ("pointing to function pointer test: % d", (* p ));

Return 0;
}

For more details, read the C language of matrix-pointer to a function.

5.9 NULL pointer

If the so-called NULL pointer is void * P, * P is a null pointer variable;

Note: void * can be converted to any type of pointer variable, which is commonly used in programming. The pointer pointing type can be used as the return type of the function.

5.10 complex statements
Char ** argv
Argv: pointer to Char
INT (* daytab) [13]
Daytab: pointer to array [13] of int
Int * daytab [13]
Daytab: array [13] of pointer to int
Void * comp ()
Comp: function returning pointer to void
Void (* comp )()
Comp: pointer to function returning void
Char (* X () []) ()
X: function returning pointer to array []
Pointer to function returning char
Char (* X [3]) () [5]
X: array [3] of pointer to function returning
Pointer to array [5] of char

Remember: no matter how many layers of declarations there are, you can just strip them from the variable name layer by layer!


[Disclaimer:

1) This content may come from the Internet, or I have sorted it out by myself. It only represents the opinions and opinions of the Internet and individuals!
2) This content is for reference only. Any reference to this content will cause any consequences and will be irrelevant to the original author and the author of this blog !]


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.