C Language Pointers

Source: Internet
Author: User

How variables are accessed directly

Access the value of a variable by its address (name/variable nickname)

How to access variables indirectly

Place the address of the variable in another memory unit, get the address of the variable in the other memory unit, and then locate the variable and access the data by the address of the variable.

The variable pointer is a pointer variable (itself a variable) that stores the memory address of another variable (the memory address of I), and we say pointer points to I

One variable stores the memory address of another variable, and we say it points to I (another variable)

What is a pointer

The concept of a pointer: the address of a variable is called a pointer to the variable

Pointer variable: A variable specifically used to store other variable addresses

The difference between a pointer and a pointer variable is the difference between a variable value and a variable.

Three elements of a pointer variable

    • The type of the pointer variable: consistent with the type of the variable it refers to
    • The value of the pointer variable: the address of another variable in memory
    • Name of pointer variable: starting with normal variable
Assignment of pointer variables:

The address of float *P1=P1

Mode one: pointer variable name =& variable name; &-> Fetch address character

Method Two: Assigning a value by another pointer variable

int I, *P1,*P2; Declares a variable i, pointer p1, and pointer p2

Assignment method One: p1=&i

Assignment method Two: P2=p1

Operation of pointers 1, arithmetic operations

For example: int *p,i;

Char *q,c;

p=&i;

q=&c;

The memory address shows the following meanings:

type int occupies 2 bytes, char occupies 1 bytes

Request: p++, q++

p++ The address of I is shifted 1 bits (2 bytes)

q++, that is, the address of C moves 1 bits (1 bytes)

2. Comparison operation

The name of the array is a pointer

3, two operators

*: Find the contents of the memory address later

&: To find the memory address of a variable

Pointer_1 point to A,pointer_2 point B

-----------------------Split Line---------------------

Practice

Q: If Pointer_1=&a has been executed, what is the meaning of &*pointer_1?

A: Pointer_1=&a, when pinter_1 stores the address of a,

*pinter_1 is the value that the pointer points to, which is a

So &*pinter_1 is the address of a, equivalent to &a

* Take the content,& address, the two are mutually different, cancel each other

Q: What is the meaning of *&a?

A: &a takes the address, * The address takes the content, so *&a is a

Q: What is the equivalent of (*pinter_1) + +?

Answer: *pointer_1 is the value that pointer_1 points to, so this is equivalent to a++

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.