I also want to learn C language-Chapter 2: Composite data types-pointers and pointer variables (1)

Source: Internet
Author: User
Tags define null

Pointer definition:

A pointer is a composite data type. It is a description of a type of variable. It's like the street number! For example, there is an X Street with 1024 houses. Then, each house can be indexed using X [0-1024. If every house can save an integer value, there will be 1024 different Integer Variables in street X. In addition, each variable can be accessed using the house number. In fact, the number of each house here is a pointer! If we change X to memory, and each house is regarded as an independent memory space, we can conclude that a pointer is the data type with the value of other memory object addresses.

Pointer nature:

In fact, pointers are essentially the abstraction of memory addresses. There are many methods to abstract memory addresses. As long as you can access memory objects in one way, this method can be called pointers. For example, if I want to find a person who finds him through the number in the phone book, this phone number is called a pointer (the phone book is called a pointer variable ). For example, if I find his address coordinates through GPS, this address coordinate is called a pointer (this GPS is called a pointer variable ).

Pointer variable:

The variable that stores the address of another memory object is called the pointer variable.

Key Points of pointer variable declaration:

If we declare a pointer variable int * p, but at the beginning we cannot determine the value of p, we must assign NULL to p; the essence of NULL is as follows: # define NULL (void *) 0) forced conversion 0. because the default initial value of the pointer variable in the debug environment is full C, and the release environment is the last discarded value.

Difference between pointer and address:

The address indicates the number of the memory. The pointer has two information items, one is the address information, and the other is the explanation information of the address. For example, int * p indicates that the memory object pointing to the address is parsed using int. float * q indicates that the memory object pointing to the address is parsed using float.

Direct reference of C language:

Variables are directly referenced by the variable name. For example, a = 123. The Compiler automatically converts the variable name to the storage address of the variable. Then, put 123 in the bucket of variable.

Indirect reference of C language:

First, store the variable address in a variable (pointer variable), and then reference the variable by storing the pointer variable of the variable address.

Code examples for direct reference and indirect reference:

# Include <stdio. h> void main () {int a, B; int * p; // defines the pointer Variable p = & B; // store the address of variable B in Variable p a = 3; // directly reference variable a * p = 5; // indirectly reference variable B}
Memories of indirect reference:

Do you still remember the array name passing parameters we learned a few days ago? The copied copy is the first address of the array. In our implementation function, the address is readdressable.

Divergence of thinking about array names:

Can we get the address for the array name ?!

In fact, it does not make sense to get the address of the array name. The array name is an address constant. For example, constants such as 123,321 do not have memory space in the memory. Only variables have memory space. Constants are integrated into commands and there is no memory space dedicated to saving them.

Why does constant function parameters often have memory space ?!

For example, go (123), because a copy is required to save parameters.

Strictly speaking, as long as the program runs, it will occupy space. Only constants do not occupy the region where data is saved, but are integrated into commands.

 

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.