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

What does the pointer mean in C language?

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

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the meaning of pointer in C language, which is very detailed and has certain reference value. Friends who are interested must finish it.

What is the pointer?

A pointer is a variable that contains a memory address, which is the location of another object (usually another variable) in memory. For example, if one variable contains the address of another variable, we say that the first variable points to the second variable.

I believe members may be a little confused when they read the above paragraph, and there is no hurry. I will explain it to members later. Here, let me first tell you how data is stored and read in memory.

1. Storage of data in memory

If a variable is defined in the program, the system allocates memory units to the variable when the program is compiled. The compiler allocates a certain length of space according to the variable types defined in the program.

So, where are these bytes allocated in memory? How do we find it?

To solve this problem, we give each byte of the memory area a number, and this is their "address". It is equivalent to the room number in the hotel, and the data stored in the memory unit marked by the address is equivalent to the passengers living in the hotel room.

So the pointer is a variable that stores the address (number) of the memory unit.

two。 How big is a small unit?

1. For a 32-bit machine, assuming that there are 32 address lines, it is assumed that each address line produces high (high voltage) and low (low voltage) levels (1 or 0) when addressing.

The electrical signals on the 2 address lines are converted into digital signals represented by (1A 0), so it is possible

0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

That is, there is a 2 ^ 32 number, which means that 2 to the power of 32 units can be managed.

There are addresses of 2 to the power of 32.

If each address is identified as a byte, then we can give

(2 ^ 32Byte = = 2 ^ 32 / 1024KB = = 2 ^ 32 / 1024here 1024MB address = 2 ^ 32 / 1024/1024/1024GB = = 4GB) 4G is free to address.

According to the same method, we can calculate the 64-bit machine, so let's give the conclusion directly.

1. On a 32-bit machine, the address is 32 zeros or 1s in a binary sequence, so the address has to be stored in 4 bytes of space, so the size of a pointer variable should be 4 bytes.

2. On a 64-bit machine, if there are 64 address lines, the size of that pointer variable is 8 bytes to store an address.

The size of the pointer is 4 bytes on 32-bit platforms and 8 bytes on 64-bit platforms.

Second, pointer variable 1. What are pointer variables?

Think about a question, in the compiler, how to assign 3 to the variable I?

The first approach is to send 3 directly to the unit represented by I, such as "iTunes 3"

Int main () {int iTunes 3; return 0;}

The second method is to send 3 to the unit that the variable p points to (that is, the storage unit of the variable I, that is, the address, such as pendant 3, where I represents the object that p points to).

Int main () {int ibank / int I = 3: the first method int * p = & ibank / the second method / / here we take out the address of the variable a, using the & operator. / / put the address of I in the p variable, which is a pointer variable. * p = 3; printf ("% d\ n", I); return 0;}

two。 Pointer type

Think about one question:

Assign the int variable an and float variable b to the storage unit starting at 2000, are the information of & an and & b exactly the same?

The answer is different, because although the storage units have the same number, they have different data types.

In addition, because of the different data types, it is impossible to determine whether to take information from one byte (character data), from two bytes (short integer), or from four bytes (integer). The way of storage is different.

If we want to save & num (the address of num) to p, we need to give the pointer variable the appropriate type.

As follows:

A pointer of type char * pc = NULL;//har* is used to hold the address of a variable of type char. A pointer of type int* pi = NULL;//int* is used to hold the address of a variable of type int. The pointer of type short* ps = NULL;//short* is used to store the address of the variable of type short long * pl = NULL;float * pf = NULL;double * pd = NULL

As you can see here, the pointer is defined as: type name * pointer variable name.

[summary]

The address in C language includes location information (memory number, or pure address) and the type information of the data it points to, or it is "address with type", such as & a, which is generally called "address of variable a", but exactly, it is "address of integer variable a".

3. The role of pointer types

Function one:

The pointer type determines how many bytes are accessed at a time (the amount of memory accessed) when the pointer is dereferenced.

Int main () {int a = 0x11223344; int* pa = & a; * pa = 0; return 0;}

Int main () {int a = 0x11223344 * int* pa = & a; * pa = 0 * pc = & a * * pc = 0; return 0;}

Meaning of pointer type 1

The pointer type determines how many bytes are accessed at a time (the amount of memory accessed) when the pointer is dereferenced.

Char* pointer dereferencing access 1 byte

4 bytes of int* pointer dereferencing access

Function two:

The pointer type determines the step size when the pointer is an integer (skip a few bytes when the pointer is an integer).

Int main () {int a = 10; int* pa=&a; char * pc = & a; printf ("% p\ n", pa); printf ("% p\ n", pc); printf ("% p\ n", pa+1); / / if the integer pointer int*,+1 skips 4 bytes, printf ("% p\ n", pc+1) / / char* pointer + 1, skip 1 byte return 0;}

Third, wild pointer 1. What is a wild pointer?

Concept: the wild pointer is the position where the pointer points to is unknowable (random, incorrect, undefined)

What does it mean? For instance

You pick up a key, but you don't know which door it will open.

two。 The cause of the wild pointer is 2.1. Pointer not initialized

The pointer is not initialized and contains random values

# include int main () {int * pashing / local variable pointer is not initialized, default is random value * p = 20; / find a space by using random value stored in p as address, this space does not belong to our current program, resulting in illegal access / / if illegal access, p is wild pointer return 0;} 2.2 pointer out of bounds

The pointer crosses the boundary and causes the problem of wild pointer.

Int main () {int arr [10] = 0; int I = 0; int * p = arr; for (I = 0; I

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

Development

Wechat

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

12
Report