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 is the wild pointer of C language?

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

Share

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

This article mainly introduces "what is the wild pointer of C language". In the daily operation, I believe that many people have doubts about what the wild pointer of C language is. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the question of "what is the wild pointer of C language?" Next, please follow the editor to study!

What is the pointer?

The pointer is just a noun, and the pointer is the address.

We usually talk about pointers, and we can also point pointer variables.

What do you mean? Type name pointer variable = address; for example: int* pa = & a yin te pointer / our pointer type here is called yin te I read as (type asterisk). / / pa is a pointer variable

This expression means that a pointer variable pa is defined and the address of an is stored in it. The pointer variable is of type int*.

Then there are students wondering, what is the pointer variable?

What is the pointer variable?

Quite simply, we learned how to define the integer variable a before.

For example, define an "integer" and "variable a" and initialize a to 10.

Int a = 10

But now it's changed, and we've learned about pointers.

You can define a "int*" and "variable pa" and then initially recognize pa as & a.

Note:

Int* is a type. Called pointer type.

Pa is called a pointer variable.

Int* pa = & a; what is the pointer type?

Since variables have different types, such as integers, floating-point types, etc.

Then there are also different types of pointers.

Char * pc = NULL;int * pi = NULL;short * ps = NULL;long * pl = NULL;float * pf = NULL;double * pd = NULL;//NULL is a null pointer.

As you can see here, pointers are defined as type + *.

In fact:

The pointer of the char* type is to hold the address of the variable of the char type.

The pointer of the short* type is to hold the address of the variable of the short type.

The pointer of the int* type is to hold the address of the variable of the int type.

The significance of pointer types

There are so many pointer types, what is the meaning of pointer types?

Let's start with two important conclusions:

The type of pointer determines how big the pointer is (how many bytes it can go) one step forward or backward (that is, address + 1).

The type of pointer determines how much permission you have when dereferencing the pointer (how many bytes you can manipulate).

For example, char* 's pointer + 1 (that is, address plus 1) can only skip one byte, while int* 's pointer + 1 (address + 1) can skip four bytes.

Because the char type occupies 1 byte in memory, the int type occupies 4 bytes in memory.

Another example: the pointer dereference of char* can only access one byte, while the dereference of int* pointer can access four bytes.

We can't talk about it. Let's take a look at the following example.

Int main () {int n = 10 pc / define integer variable n, initialized to 10 char* pc = (char*) & n Bash / define character type pointer variable pc, address initialized to n int* pi = & n pc / define integer pointer variable pi, address initialized to n printf ("% p\ n", & n); printf ("% p\ n", pc) Printf ("% p\ n", pc + 1); printf ("% p\ n", pi); printf ("% p\ n", pi + 1); return 0;}

The printed result is as follows, which is a hexadecimal number. You can see that the pointer variable pc plus 1 of the character type can only operate on one byte, so it changes from 4x1 to 5. The integer variable pi+1 skips 4 bytes and becomes 8.

To sum up, this is the meaning of the pointer type.

What is the wild pointer?

Hearing the wild pointer, some people may wonder what this noun means.

Don't worry, the wild pointer is easy to understand.

If a dog is described as wild, it can be understood that the dingo often occupies other people's territory, or appears randomly anywhere.

So that's what the wild pointer means.

A wild pointer is a pointer that always occupies other people's memory (address). Or an address appears at random.

The cause of the wild pointer is 1. The pointer is not initialized int main () {int * p / local variable pointer is not initialized, default is random value * p = 20; return 0;} II, pointer out of bounds access # include int main () {int arr [10] = {0}; int * p = arr; int iTun0; for (iTun0; 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