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 relationship between C language pointer, address and array function heap space?

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Most people do not quite understand the knowledge points of this article "what is the relationship between C language pointers, addresses and array function heap space", so the editor summarizes the following contents, which are detailed and clear, and have a certain reference value. I hope you can get something after reading this article. Let's take a look at this article entitled "what is the relationship between C language pointers, addresses, and array function heap space".

1. A special variable-pointer

Pointers are variables in C language.

Because it is a variable, it is used to save specific values.

What is special is that the value saved by the pointer is the address in memory

What is the memory address?

Memory is a storage unit in a computer, and each memory unit has a fixed and unique number.

The number of the storage unit in memory is the memory address

The facts that need to be clarified.

All elements in the program exist in memory, so the program elements can be accessed through the memory address.

Memory example

Get the address

In C language, the address of program element is obtained by & operator.

& you can get the starting address of variables, arrays, and functions

The essence of a memory address is an unsigned integer (4 bytes or 8 bytes)

Here's a simple example:

# includeint main () {int var = 0; printf ("var value =% d\ n", var); printf ("var address =% p\ n", & var); return 0;}

The following is the output:

Matters needing attention

The value saved in a variable can only be determined by the memory address + length.

Pointer definition syntax:

Type * point

Type-data type that determines the length range when accessing memory

* flag, which means defining a pointer variable

Pointer variable name, following C language naming rules

For example:

Int main () {char* pChar; short* pShort; int* pInt; float* pFloat; double* pDouble; return 0;}

Pointer memory access:

* pointer

The pointer access operator (*) acts on the pointer variable to access in-memory data

The type of pointer determines the length range when memory is accessed by address

The types of pointers occupy 4 or 8 bytes.

Namely: sizeof (type*) = = 4 or sizeof (type*) = = 8

Take a look at a piece of code and feel it:

# include int main () {int var = 0; int another = 0; int* pVar = NULL; printf ("1. Var =% d\ n", var); printf ("1. PVar =% p\ n", pVar); pVar = & var; / / address where the pointer is used to save variables * pVar = 100; / / * pVar is equivalent to var, var = 100; printf ("2. Var =% d\ n", var) Printf ("2. PVar =% p\ n", pVar); pVar = & another; / / changed the direction of pVar so that pVar keeps the address of another * pVar = 1000; / / another = 1000; printf ("3. Another =% d\ n", another); printf ("3. PVar =% p\ n", pVar); printf ("4. Add = >% d\ n", var + another + * pVar) / / 1000 + 1000 = > 2100 return 0;}

The following is the output:

Note that the NULL address is 00000000

Summary

Pointers are variables in C language (essentially containers)

The pointer is dedicated to the memory address of the program element

You can use the * operator to access the program element itself through a pointer

Pointers also have types, and pointer types are made up of data types + *

Second, in-depth understanding of pointers and addresses

Three questions of the soul

What is the relationship between pointer types and normal types?

What do you think of "memory address + length to access data in memory"?

Can different types of pointers be assigned to each other?

Beginner to learn the military rules of pointers

A pointer of type Type* holds only the address of a variable of type Type

Prohibit different types of pointers from assigning values to each other

It is forbidden to assign ordinary values to pointers as addresses.

Note: the address saved by the pointer must be a valid address

Let's look at a piece of code:

# include int main () {int I = 10; float f = 10; int* pi = & f; / / WARNING float* pf = & f; / / OK printf ("pi =% p, pf =% p\ n", pi, pf); printf ("* pi =% d, * pf =% f\ n", * pi, * pf); pi = I; / / WARNING * pi = 110 / / OOPS printf ("pi =% p, * pi =% d\ n", pi, * pi); return 0;}

The following is the output:

The program made two mistakes:

1. Assign different types of pointers to each other. Although the address of the pointer variable of int type is correct, the value it holds is wrong.

2. Assign an ordinary value to the pointer as an address, which will lead to a serious error and cannot be output correctly

Write a function to exchange the values of two variables

If you want to write a function to exchange the value of a variable, you must have the ability to modify variables outside the function inside the function!

Look at the following code:

# include void func (int* p) {* p = 100; / modify 4 bytes of data in memory, that is, modify the value of an integer variable} void swap (int* pa, int* pb) {int t = 0; t = * pa; * pa = * pb; * pb = t;} int main () {int var = 0; int a = 1, b = 2; printf ("1. Var =% d\ n", var) Func (& var); printf ("2. Var =% d\ n", var); printf ("3.a =% d, b =% d\ n", a, b); swap (& a, & b); printf ("4. A =% d, b =% d\ n", a, b); return 0;}

The following is the output:

Small conclusion

You can use pointers to "return" multiple values from a function (return can only return one value)!

Let's look at a piece of code:

# include int calculate (int n, long long* pa, long long* pm) {int ret = 1; if ((1

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