Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to assign pointer variables in C language

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to assign pointer variables in c language". In daily operation, I believe that many people have doubts about how to assign pointer variables in c language. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt of "how to assign pointer variables in c language". Next, please follow the editor to study!

Overview of pointers:

The pointer is flexible and convenient to use, and can make the program concise, efficient and compact. It can be said that the pointer is the essence of C language.

Pointer involves the physical storage of data, the concept is complex, the use of flexible and error-prone, so it is difficult to master. Learning pointers should grasp the basic concepts and use diagrams to analyze and solve problems.

Simply put, address (memory space or variable)-pointer; address variable-pointer variable

Address and pointer

Take a byte of the memory as a memory unit, and the integer type accounts for 4 bytes, so the integer data accounts for 4 memory units, and the character type occupies 1 byte, so the character data accounts for 1 memory unit.

Number each memory unit, and you can accurately find the memory unit according to the number, which is called the address.

Compare the memory to a hotel, the memory unit to the room in the hotel, then the address is like the house number of the room.

A pointer is a type of variable that is used specifically to store addresses.

For example: int I = 2

Variables and pointers

The address of a variable is the link between the variable and the pointer.

The pointer is the address that points to a variable

The address of a variable is called the pointer of the variable

The variable that holds the address of the variable is the pointer variable.

A pointer type is a variable type used specifically to store the address of a memory unit.

Pointer variable

1. General form:

Type specifier * variable name

"*" indicates that the variable is a pointer variable

Variable name is pointer variable name

The type descriptor indicates the data type that the pointer variable points to

Int * p

Char * cp

two。 Assignment of pointer variables:

Pointer variables must be assigned before they are used

The value of a pointer variable can only be an address, not another data type

Address operator & is the operator used to get the address of a variable, for example: & a

Method of assignment: (suppose there is int a = 6;)

(1) complete initialization when defining: int * p = & a

(2) define first and then assign: int * p; p = & a

3. Reference to pointer variable

The dereferencing operator "*" can dereference the pointer variable and get the value pointed to by the pointer variable.

Dereference form: * pointer variable

For example:

Int a = 6

Int * p = & a

Int b = * p

4. & and * operators

(1) "&" and "*" are both unary operators with the same priority and are right associative.

(2) "&" is the address operator used to obtain the address of the variable, and "*" is the dereferencing operator used to dereference from the pointer variable to get the value it points to.

Int a = 6

Int * p = & a

& * p is equivalent to p

* & an is equivalent to a

At this point, the study of "how to assign pointer variables in c language" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report