Complete C language pointer first article

Source: Internet
Author: User

1. essence of variables in Language

To understand the C pointer, I think we must understand the storage essence of "variables" in C. So let's start with "variables!

Let's first understand the memory space! See:

Memory Address → 6 7 8 9 10 11 12 13

-----------------------------------------------------------------

... | ..

-----------------------------------------------------------------

The memory is just a space for storing data, just like the seats in the cinema when I watch a movie. Each seat is numbered, and our memory needs to store all kinds of data. Of course, we need to know where our data is stored! So the memory should be numbered like the seat, which is what we call the memory addressing. A seat can start with number 1 for one seat and a number for one seat, and the memory is edited by one byte and one byte, as shown in. Each byte has a number, which is called the memory address. Well, I have said so much. Can you understand the concept of memory space now?

Let's continue to look at the following statements about C and C ++ language variables:

Int I;

Char;

Every time we use a variable, We need to declare it in advance, it is actually a space in the memory that applies for the width of an integer variable named I (the width of the 16-bit programming in DOS is two bytes ), and the space (in bytes) of the width of a variable named ).

How can we understand how variables exist. When we declare variables as follows:

Int I;

Char;

The image in the memory may be as follows:

Memory Address → 6 7 8 9 10 11 12 13

------------------------------------------------------------------

... | ..

------------------------------------------------------------------

Variable name | → I branch | → A branch |

As shown in the figure, I applied for two bytes of space at the memory start address of 6 (I assume that the int width is 16 bits, in different systems, the int width may be different.) and name it I. A applied for a one-byte space on the memory address of 8 and named it. in this way, we have two different types of variables.

2. assign values to variables

Let's look at the following assignment:

I = 30

A = 'T'

Of course, you know that two statements are to save 30 to the memory space of the I variable, and store the 'T' character into the memory space of the variable. We can understand this image:

Memory Address → 6 7 8 9 10 11 12 13

-----------------------------------------------------------------------

... | 30 | 'T' | ..

-----------------------------------------------------------------------

| → I branch | → A branch |

3. Where are the variables? (I want to know the variable address)

Now, let's take a look at what I mean?

Is it the address number where the I variable is located! We can read it like this: return the address number of the I variable. Do you remember?

To display the address value of a variable on the screen, you can write the following:Code:

Printf ("% d", & I );

In the example of a memory image, the screen displays 6 instead of I value 30. Of course, in actual operations, the address value of the I variable will not be this number.

This is the essence of variable storage that I think should be imagined by beginners. Please understand this!

The final summary code is as follows:

 IntMain ()
{
IntI =39;
Printf ("% d \ n", I );//①
Printf ("% d \ n", & I );//②
}

Now you know what I is output on the screen by the two printfs?

Okay! Next we will start learning the pointer. Come on! (To be continued ...)

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.