C--(10) Pointer basics

Source: Internet
Author: User


Knowledge Points:
1. Pointer basics
2. Pointers and functions (address passing)

=======================================

pointers are an important concept in C and an important feature of C. Mastering the application of pointers can make the program concise, compact and efficient.
The concept of pointers is more complex, use is more flexible, so beginners will often error, we have to understand the nature of each concept, more practice, in practice to master it.

The program uses pointers and code that does not use pointers is two grades.

========================================
Pointer Basics
"Address Concept"

1. The concept and role of address in life
(a) The number of the House, to find the person by address.

(b) Address concept and function in the program (number of memory)
During program execution, all data objects are stored in the computer's internal memory.
Any data object has a certain storage location during the period during which it is executed, occupying a certain number of storage units.
And each storage unit has a unique number, which is the address.

2. What is a pointer
Since the address can find the desired storage unit, we can say that the address points to the variable unit. In c the address image is called the "pointer"
1) The essence of the pointer is a memory address

3. Pointer action
0) can operate the memory directly
1) Make the program more concise and efficient

======================================
Address and data

1. Common data manipulation is the storage and access of data, data can be stored through variable units, each variable has a certain data type, the system according to the data type of the allocation of variables to the size of the storage unit.

2. Variable type, size, stored value (review)
char c;
Short A;
int b;
Long D;

1) How to get the size of variable storage space
int A;
sizeof (a) = sizeof (int);

2) How to get the stored location
Each variable has its corresponding address, when accessing the variable, the system first detects the corresponding address according to the variable name and then operates the storage unit corresponding to the address.

& is the address character, you can get the address of the storage unit


Exercise: Print the address of the variable a,b,c (%p) separately.
The parameter notation in 1>SCANF

Access to a storage unit by variable name is called Direct access .


======================================
Pointer variable
The address of a variable can be called a pointer to a variable and is a 16-based value.
If a variable is specifically defined to hold the address of another variable, the variable is called a pointer variable .

1) Declare pointer variable store address
How to declare pointer variables
type * variable name;

2) The nature of the pointer variable
1> Special variables, nature is still a variable
2> stored data is the memory address of the other variable
Practice declaring a pointer variable

3) How to assign value to a pointer variable
Assigning a value to a work variable
Assigning values to pointer variables


Pointer variable p holds the address of the variable a called the pointer p points to the variable a

4) How to access the contents of the specified memory through pointer variables
Using the * number is (indirect access)

* and & are mutual inverse


5) Note the difference between memory unit address and Memory unit content!!
Thinking: &p, P,*p is the same content

6) Practice:
1. Compare the size of integers by pointers
2. Exchange values of two variables by pointer

========================================
1. Deep analysis of pointer variables
1) The pointer is a memory address, and the pointer variable is a variable that holds the pointer. This variable holds the address of another variable
2) pointer variable also has variable name, address, size and stored value
int *p;
2. Various types of pointers and sizes
1) Exercise: 1. Declaring pointer variables of various types
Char char *
Short short *
Int int *
Long Long *


2) How to get the memory size of the pointer variable
Explore: 1. Get the size of other types of pointer variables, see what the difference is, and summarize
Mac system is a 64-bit system, a pointer variable, accounting for 8 bytes
Windows XP is on a 32-bit system: A pointer variable that accounts for 4 bytes

==========================================
pointer variables using
1. Thinking: What do I need to be aware of before using variables?
P &p *p

2. Pointer variables use the trilogy
1) To declare a pointer variable
2) Initialize the pointer variable
3) Access the variable pointed to by *p

3. Initialization of the pointer
1) If the pointer variable is not initialized to how the
uninitialized pointer is called a wild pointer, use a large bogey of the pointer.
2) Initializes the pointer variable to NULL, which means that the pointer is dangling and the pointer is no longer able to access any data. int *p = NULL; printf ("%p,%d", p,*p);

4. How to safely use pointer variables
to determine whether a null pointer is used before use, the wild pointer is an indeterminate address, not equal to null


=====================================
Why do you use pointers
1. The value of the argument can be modified by parameter (address delivery)
2. Working with strings

=====================================
Pointers and functions
1. Swap function when recalling function (value pass)
2. Modifying an argument with a pointer variable (address passing)

Eg: a function can achieve two-digit exchange
Practice:
2. Write two functions
1) Enter the three number to return and, finally need to set the input content all to 1
add int A, B, c to output, and a,b,c to 1
int add (int *x, int *y, int *z)

2) Implement a module that converts the uppercase letters entered into lowercase letters

=====================================
Const retouching
Requirement: The project now needs to set read-only to certain variables to protect the data from corruption
1.const Modifying the function of ordinary variables
The role of 2.const modifier pointers

1) const before the * number, this time the modifier is a *p variable, which indicates that the memory area pointed to by the variable is read-only

2) const after the * number, this time the modification is the P variable, indicating that the value of the variable itself does not allow modification


The role and usefulness of 3.const modified variables
1) typically appears in the parameter list of the function, which is called by the caller, and the value of the parameter is not modified during function execution


=====================================
Extended
1. How to declare multiple pointer variables at the same time in one line of code
int A, b, C;

int * P,Q,M; the wrong wording
int * p, * q,* m

C--(10) Pointer basics

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.