pointer beginner (definition, initialization, heavy pointing, precautions)

Source: Internet
Author: User

Hello everyone, today we will learn the pointers in C language. Perhaps everyone has heard such a sentence: C language in other I learned very well, is the pointer this piece of learning is not too clear, then you might as well tell me you have not learned C language. This sentence highlights the importance of the concept of pointers in the C language. Some students said, I do not speak from the C language of the pointer is very difficult, particularly difficult to understand, difficult to learn. So what I'm telling you is that there is nothing difficult with pointers, that's just what your subconscious thinks, and through this lesson today, I'm sure we can all learn pointers and use pointers. OK, here's the beginning of the pointer learning.

Before learning the pointer, I will first give you the basic knowledge, basic concepts. 1. What is memory space? 2. What is an address? 3. What is direct access? 4. What is indirect access?

1. What is memory space? What is an address?

As the name implies: is the space to store data. For the moment, we think of bytes as the smallest storage unit, and our memory space is made up of a lot of bytes. For example: 2G memory = 2 of 31 powers-1 bytes. We number each byte, each byte is numbered, and the byte number, which is the memory unit number, is the memory address.

We can give an example of life, such as: The cinema is equivalent to a piece of memory, and the cinema is composed of many seats, and one seat is the equivalent of memory bytes, and we also numbered each seat, each seat has a number, and the number of each seat, is equivalent to the memory address.

2. What is direct access? Code:

int A; We have requested a memory space in memory, and named A, the type of storage is integer type

A = 5; Initialize the memory space you just requested, and store the content as Integer 5

char c; We have requested a memory space in memory, and the name is C, the type of storage is character type

c = ' A '; Initializes the memory space you just made, and stores the contents as a character a

A = 10; Change the storage content to 10 (write data)

printf ("%d\n", a); (Reading of data)

Definition: We access the corresponding storage space directly through the variable name (read and write data)

Figure:

Address memory space

FFC1 A = 5

Ffc5 c = ' A '

3. What is indirect access? Definition: We access the contents of the storage space through the memory address and the size of the number of bytes that the variable occupies.

Address memory space

FFC1 A = 5//FFC1 is the memory address of variable A, and the size of the bytes is 4 bytes

Ffc2

Ffc3

Ffc4

4. How do we get the address of a variable? Then one operator is involved: the FETCH operator (&)

&a; So we get the memory address of variable A.

We got a memory address, so how do we print it?

printf ("%p\n", &a); %p We print an address by using the pointer's formatted output character%p

So always come, how do we access a piece of memory space by memory address and the size of the number of bytes, and then print out? So that's what we're going to talk about today?

5. What is a pointer? The definition of a pointer? The initialization of the pointer?

6. What is a pointer? Give two examples of life 1). For example, we have a movie theater in the above examples, we go to the cinema to see the movie there is a film ticket, the movie ticket has our seat number, we can use the seat number to find our seats, then I say now, this movie ticket is the pointer. 2) Let's give an example a little bit, right, so let's take another example, if you come to me to borrow a book, but I'm not here, but I left you a note on the table that tells you that my book is on the second floor of the bookshelf, the third one you want to borrow, and you can find the book you want to borrow through this note, right? So now I'm telling you, this note is a pointer.

So how do we define the pointers in C language? Definition: A variable that holds the address of a variable, which is a pointer. So how do we define a pointer?

int * p; OK, now we define a pointer, we see, wow, how difficult ah, how many a * number, then I tell everyone is not difficult, why not difficult, listen to my explanation. First of all, it's also a variable, it's not fundamentally different from the variables we defined earlier, and the * number just indicates that we're defining a pointer variable.

Figure:

Address memory space

FFC1 A = 5

FFC5 P=FFC1

As you can see from this graph, pointer variables are also addressed and have memory space, just like the variables we defined above. And it is not the same as the variables we defined above, it stores the content is not the same as the above variables. As we mentioned above, the stored content of the pointer variable is the address, so how do we get the address out and assign to the pointer variable? This involves the initialization of pointers?

7. Initialization of pointers?

p = &a; This sentence means that we take the address of the variable A to the pointer variable p, so that we have completed the initialization of the pointer, also known as the pointer variable p is pointed to the variable A.

So we go through the pointers to indirectly access memory space? Then the * (value operator) is used to obtain the contents of the storage space corresponding to the address.

8. *p; The meaning of this sentence is to obtain the corresponding storage space of the pointer P (the address of variable a) 5

printf ("%d\n", *p); Output value of 5 reads data indirectly through pointers

We can compare with the previous example, this pointer variable p, equivalent to our previous note, and P value, equivalent to the content on the note, and variable a corresponding storage space, is the book we are looking for, so you understand?

*p = 10; To write data indirectly through pointers

9. What is the direction of the pointer?

We all know the value of a variable, can be changed, the position of the pointer to change it? Yes, and this is called the pointer's heavy pointing.

int B = 20;

p = &b;

printf ("%d\n", *p); Now our pointer variable is pointing to variable B, so we're going to print a result of 20.

10. Pointers Considerations

int * p;

1. p = 100; Error: Our p is a pointer variable, it holds an address, and 100 is a constant

2. *p = 100; Error: Our p is simply defined and has not been initialized, that is, we do not know which piece of content it is pointing to, and we are going to change its value.

3. What is the number of bytes that the pointer occupies? The size of the number of bytes that the pointer occupies is independent of the type, related to the number of bits in the compiler, the 32bit compiler environment, the pointer is 4 bytes, the 64bit compiler environment, and the pointer takes up 8 bytes

4. Why does the pointer want to define the type? The pointer points to a variable of the same type, or the data is read with an error.

pointer beginner (definition, initialization, heavy pointing, precautions)

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.