The meaning of a pointer--pointer variable, pointer pointer, pointer use note

Source: Internet
Author: User

Pointers are really easy to understand, and others are not as magical and difficult as everyone imagined.

first, the meaning of the pointer: The pointer is actually a variable, it does not hold the data, but the data in memory address. Of course the pointer can also hold the address of the function (method), which will be mentioned later. The key for declaring a pointer is the * number, and the key to the address is &.

Second, pointer variable:

Example 1: #include <stdio.h>

int main

{

int a = 10; The 11th variable, a, is assigned a value of 10

int *p = &a; P is a pointer variable that points to the address of variable a

printf ("%d\n", p); The value of the variable A that the output pointer variable p points to that address

}

For example, 2 modifies the value of a variable by pointer:

#include <stdio.h>

void change (int *a);

int main

{

int a = 10; The 11th variable, a, is assigned a value of 10

Change (&a); P is a pointer variable that points to the address of variable a

printf ("%d\n", a); The value of the variable A that the output pointer variable p points to that address

}

void chage (int *a)

{

*a = 20; Modify the value of a by using the pointer

}

Pointers to pointers: by the pointers described above and the meaning of the pointer variable, the pointer to the pointer to the literal understanding of the people should be able to understand it, here is an example:

int mait

{

int a = 20; The 11th variable, a, is assigned a value of 10

int *p = &a; P is a pointer variable that points to the address of variable a

int **pp = &p; PP is a pointer variable, pointing to the variable pointer variable p address (&p represents the address of the pointer variable p)

printf ("%d\n", **pp)//The value of the output is the value of a

}

Output: 20

As the above example says, you will understand better *pp = = p; Pointer variable *pp find the storage space for pointer variable p

* (*pp) = *p = A; Pointer variable *pp Find the value of the address pointed to (both the same)

**PP = *p = A; Pointer variable *pp Find the value of the address pointed to (both the same)

Four, the use of pointers to three major notes:

①: pointer types can only point to data of the same type

Error Demo 1:

#include <stdio.h>

int main

{

int *p;

Double d = 10.0;

p = &d;

printf ("%d\n", *p); No error, only warning!

}

②: Pointer variable can only store address

Error Demo 2:

#include <stdio.h>

int main

{

int *p;

p = 200; P can only store addresses, unless 200 is an address

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

}

③: Pointer variable is not initialized, do not use indirect access to other storage space

Error Demo 3:

#include <stdio.h>

int main

{

int *p;

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

}

The meaning of a pointer--pointer variable, pointer pointer, pointer use note

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.