Simple learning C-day 6 and day 6

Source: Internet
Author: User

Simple learning C-day 6 and day 6

 

Pointer

Pointer is a flexible content in C language. Of course, flexibility is hard to grasp. However, as long as you understand the essence and learning, it is easy to use pointers.

First understand, 1. What is a pointer?

In C, pointers are also the same data type as Int and double. int stores an integer number and double stores a double number,The pointer stores the address.. I remember the example of an image like a warehouse number. This room number represents the specific location of the room.

2. What is the usage of pointers?

Because the pointer stores the address, the pointer can directly access and pass the data on that address and that address. This is undoubtedly what we will talk about next time.FunctionGreat convenience. For a preliminary understanding of the pointer, you still need to write a few small programs to observe and then you will know.

Before writing a program, you need to know how to use pointers.

Because pointer is also a data type, its usage is similar to that of int. First, 1. Define the pointer in the formatData Type (such as int, double, struct) + * identifier; the meaning of the data type is, what data type is pointed to by the address of the pointer you define nowFor exampleInt * p;It defines a pointer variable pointing to an integer.

So, let's write a small program to explore it.

Code:

 

# Include <stdio. h> int main () {int a; // defines the integer scanf ("% d", & a); while (a --) {int * p; // define the integer pointer Variable p = &; // assign the address of a to the pointer Variable p // now p stores the address printf ("% d --", * p); // output the content of printf ("% d \ n", p) in the address pointed by p; // output the address of p }}

Running result:

From the code above, we can clearly see that it refers to the address where the variable is stored. So what is the pointer address? Add a sentence before the last statement of the above programPrintf ("% d --", & p );This is the result after running.

Visible pointers also need to be stored in memory units.

There are still some unfamiliar symbols,* Two scenarios are available: 1. it is used in definition to define a pointer variable (such as int * p), 2. in the expression, it indicates getting the content symbol and getting the content stored in the address where p is stored (such as * p ). Also, it is used as the get address to get the address where the content is stored (for example, & p ).

Exchange the two numbers and output them. I believe that each reader will write this program independently,TroubleFor the sake of consideration, can we exchange the two numbers through the address operation? Of course.

Code

# Include <stdio. h> int main () {int a, B; int * p, * q, * w; // defines the pointer variable scanf ("% d", &, & B); p = & a; // p address for storing variable a q = & B; // q address for storing variable B printf ("1: % d \ n ", * p, * q); // address exchange, which is the same as w = p; p = q; q = w; // at this time, p Stores the address of variable B, and a stores the address of variable B printf ("2: % d \ n", a, B); printf ("3: % d \ n ", * p, * q); printf (" 4: % d \ n ", & a, & B); printf (" 5: % d \ n ", p, q );}

Running result:

 

You also need to note that many things in the C language can be embedded and overlapped, which I have mentioned many times and pointers are no exception. For example, you have defined such a variable.Int ** q;What is the use of this variable? It correspondsInt * p;What is the relationship? The preceding q indicates that a second-level pointer is defined, and its function is to point to the address of the first-level pointer, as shown inQ = & p; assign the address of the first-level pointer p to the second-level pointer q.At the same time, Level 3 and Level 4 share the same principle. As long as you remember that the pointer stores the address, the content of the address is related to the corresponding data type.

 

Pointers are called the most difficult part of the C language. However, as long as you master the principles and think about them carefully, some basic problems can still be easily understood.

Related Article

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.