C language pointer variable detailed introduction _c language

Source: Internet
Author: User

The address of the data in memory is also called a pointer, and if a variable stores a pointer to a data, we call it a pointer variable.

In the C language, a variable is allowed to hold the pointer, which is called a pointer variable. The value of a pointer variable is the address of a data that can be an array, a string, a function, or another ordinary variable or pointer variable.

Now suppose you have a char-type variable C that stores the character ' K ' (ASCII is decimal number 75) and occupies an address of 0x11a memory (the address is usually represented in hexadecimal). There is also a pointer variable p, whose value is 0x11a, exactly equal to the address of the variable C, in which case we say P is pointing to C, or p is a pointer to the variable C.

Defining pointer variables

Defining a pointer variable is very similar to defining a normal variable, but precede the variable name with an asterisk *, formatted as:

DataType *name;

Or

DataType *name = value;

* Indicates that this is a pointer variable, datatype represents the type of data that the pointer variable points to. For example:

int *p1;

P1 is a pointer variable that points to an int type of data, and as to which data the P1 is pointing, it should be determined by the value assigned to it. Another example:

int a = 100;
int *p_a = &a;

When you define the pointer variable p_a it initializes it and assigns the address of variable A to it, at which point p_a points to a. It is worth noting that P_a needs an address that must be preceded by an address ampersand, otherwise it is not correct.

As with normal variables, pointer variables can be written multiple times, and you can change the value of the pointer variable at any time if you want, see the following code:

Define general variable
float a = 99.5, B = 10.6;
char c = ' @ ', d = ' # ';
Define pointer variable
float *P1 = &a;
Char *p2 = &c;
Modify the value of a pointer variable
p1 = &b;
P2 = &d;

* is a special symbol indicating that a variable is a pointer variable, and that the definition of P1 and P2 must be carried *. When assigning a value to P1, P2, because you already know that it is a pointer variable, there is no need to bother with *, and you can use pointer variables like normal variables. That is, the pointer variable must be defined with a *, and the pointer variable cannot be assigned with a *.

Assuming that the addresses for variables a, B, C, and D are 0X1000, 0x1004, 0X2000, and 0x2004, the following diagram reflects the P1, P2-pointing changes well:

It should be emphasized that the types of P1 and P2 are float* and char*, rather than float and char, which are completely different data types, and readers should draw attention to them.

Pointer variables can also be defined continuously, for example:

int *a, *b, *c; The types of a, B, and C are all int*.

Note that each variable is preceded with a *. If written in the following form, then only a is a pointer variable, b and C are common variables of type int:

int *a, b, C;

Obtaining data through pointer variables

The pointer variable stores the address of the data, which can be obtained by a pointer variable in the form of:

*pointer;

Here the * is called the pointer operator, used to get the data on an address, see the following example:

#include <stdio.h>
int main () {
 int a =;
 int *p = &a;
 printf ("%d,%d\n", A, *p); Both ways can output a value of return
 0;

Run Result:

15, 15

Assuming that the address of a is 0x1000,p to point A, the value of P itself becomes 0x1000,*p to get the data on the address 0X1000, also known as the value of variable A. From the results of the operation, *p and a are equivalent.

As we said in the previous section, CPU Read and write data must know the address of the data in memory, both ordinary and pointer variables are mnemonics of addresses, although they run in a slightly different way than the data obtained through *P and a: a requires only one operation to get the data, and *p has to go through two operations, one more layer. Indirect ".

Suppose that the addresses of variable A and p are 0X1000, 0xf0a0, and their pointing relationship is shown in the following illustration:

After the program is compiled and linked, A and p are replaced with the corresponding address. Using *p words, first, the value of the variable p itself is obtained by address 0xf0a0, which is the address of the variable a, and then the data of variable A is obtained by this value, and there are two operations before and after, and in the case of a, the data can be obtained directly from the address 0X1000.

In other words, using pointers is the indirect acquisition of data, the use of variable names is direct access to data, the former more expensive than the latter.

The pointer can also modify data on memory, in addition to the data on memory, for example:

#include <stdio.h>
int main () {
 int a =, b = =, c = 222;
 int *p = &a; Define pointer variable
 *p = b//modify data on memory by pointer variable
 c = *p;//Fetch data on memory via pointer variable
 printf ("%d,%d,%d,%d\n", A, B, C, *p);
 return 0;
}

Run Result:

99, 99, 99, 99.

*p represents the data in a, which is equivalent to a, you can assign another piece of data to it, or you can assign it to another variable.

* Different roles in different scenarios: * Can be used in the definition of a pointer variable to indicate that it is a pointer variable that is separate from the normal variable; When you use a pointer variable, the preceding * indicates the data to which the pointer points, or the data itself that the pointer points to.

That is, when you define a pointer variable, the * is completely different from the meaning of the pointer variable. Take the following statement as an example:

int *p = &a;
*p = 100;

The 1th line of code * is used to indicate that p is a pointer variable, and the 2nd line of code is used to get the data that the pointer points to.

Note that you cannot add * to the pointer variable itself when assigned a value. Modify the above statement:

int *p;
p = &a;
*p = 100;

The P in the 2nd line of code is not preceded by *.

Pointer variables can also appear in any expression that a normal variable can appear, such as:

int x, y, *px = &x, *py = &y;
y = *px + 5; To add 5 to the content of X and assign it to y,*px+5 equivalent to (*px) +5
y = ++*px;//px content plus 1 assigns to y,++*px equivalent to + + (*px)
y = *px++;//equivalent to y= (*PX) + +
py = px Assign the value of a pointer to another pointer

"Example" swaps the values of two variables with a pointer.

#include <stdio.h>
int main () {
 int a = m, b = 999, temp;
 int *pa = &a, *PB = &b;
 printf ("a=%d, b=%d\n", A, b);
 /***** begins to exchange *****/
 temp = *PA;//Save a value first
 *PA = *PB;//give the value of B to a
 *PB = temp;/and give the value of a saved A to B
 /***** End Exchange *****/
 printf ("a=%d, b=%d\n", A, b);
 return 0;
}

Run Result:

A=100, b=999
a=999, b=100

From the results of the operation, we can see that the value of a and B has been exchanged. Note that temporary variable temp is particularly important because the execution *pa = *PB, the value of a after the statement is overwritten by the value of B, and cannot be found without first saving A's value.

About * and & 's puzzles

Suppose that there is a variable of type int A,PA is a pointer to it, then what does *&a and &*PA mean respectively?

*&a can be understood as * (&a), &a represents the address of the variable a (equivalent to PA), * (&a) means to take the data on this address (equivalent to *PA), around and back to the origin, and *&a is still equivalent to a.

&*pa can be understood as & (*PA), *PA indicates that the data that the PA points to (equivalent to a),& (*PA) represents the address of the data (equivalent to &a), so &*pa is equivalent to Pa.

A summary of the asterisk *

In the syntax we have now learned, the asterisk * has three main uses:

    1. Represents multiplication, such as int a = 3, B = 5, C; c = A * b;, this is the easiest to understand.
    2. Indicates that a pointer variable is defined to be separated from the normal variable, such as int a = 100; int *p = &a;.
    3. Indicates that getting the data that the pointer points to is an indirect operation, such as int A, b, *p = &a;  *p = 100; b = *p;.

The above on the C language pointer variable data collation, follow-up continue to supplement the relevant information, thank you for your support of this site!

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.