Simple study c--Sixth day

Source: Internet
Author: User

Pointer

Pointers are a very flexible part of the C language, and of course, flexibility is difficult to master. However, as long as the understanding of its essence, learning, the use of pointers is a very easy thing.

First understand, 1. What is a pointer?

In the C language, the pointer is also the same as int, double is a data type, int holds an integer number, double holds a double-precision number, the pointer holds the address . Remember that the teacher told an example of such an image: The pointer is similar to the house number of a warehouse. This House number can reflect the exact location of the room.

2. What is the use of pointers?

Since the pointer holds the address, the pointer can be accessed directly, passing the data and that address on that address, which is undoubtedly very handy for the next function We're going to say. The initial understanding of pointers or the need to write a few small procedures to observe, and then you know.

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

Because the pointer is also a data type, its usage is almost identical to the use of Int. First, 1. Define a pointer in the form of a data type (such as int, double, struct) + * identifier; the meaning of the data type is that the pointer you are defining now holds the address of the data type , such as int *p; What it means is to define a pointer variable that points to an integer number.

So, let's write a little program to explore and explore.

Code

#include <stdio.h>intMain () {intA//Defining integral Typesscanf"%d",&a);  while(a--)   {       int*p;//defining pointer variables for integral typesp=&a;//assigns the address of a to the pointer variable p//now P is storing the number represented by variable A in the address stored in the computer.printf"%d--", *p);//The address that the output P points to holds the contentsprintf"%d\n", p);//address of output P   }}

Operation Result:

It can be clearly seen from the above code that the address of the variable is stored. So think about it, how much is the address of the pointer?  Add a sentence of printf ("%d--", &p) to the front of the last statement of the above program; After running, you can see that this is the result.

Visible pointers are also needed to store the memory cells.

There are still more unfamiliar symbols above,* There are two kinds of places to use 1. At the time of definition, the delegate defines a pointer variable, (such as int *p), 2. Appears in the expression, representing the content symbol, to obtain the contents of the address stored in P (such as *p). There is also &, which is used as a fetch address character, to get the address that the content is stored in (such as &p).

Give two number, exchange these two numbers, output these two numbers, I believe each reader will write this program independently, but for the sake of trouble , can the operation of the address to exchange these two number? Of course I can.

Code

#include <stdio.h>intMain () {intb; int*p,*q,*w;//Defining pointer Variablesscanf"%d%d",&a,&b); P=&a;//p Store The address of variable aq=&b;//Q Store The address of variable Bprintf"1:%d%d\n", *p,*q); //exchange of addresses, as in two-digit exchangew=p; P=Q; Q=W; //at this point P holds the address of variable B, and a stores the address of variable Bprintf"2:%d%d\n", A, b); printf ("3:%d%d\n", *p,*q); printf ("4:%d%d\n",&a,&b); printf ("5:%d%d\n", p,q);}

Operation Result:

It is also necessary to note that a lot of things in the C language can be mosaic overlapping, which I have mentioned many times, and pointers are no exception, for example, you define such a variable int **q; What is the use of this variable? It is *p with int; What's the matter? The previous q represents the definition of a level two pointer whose function is to point to the address of a first-level pointer, such as q=&p; assigns the address of the first-level pointer p to level two pointer Q. at the same time, level Three, level four are the same, just remember that the pointer is the address, the content of the address and its corresponding data type.

Pointers are called the most difficult part of C, but as long as you master the principle and think about it, some basic questions can be easily understood.

Simple study c--Sixth day

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.