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

C language pointer dynamics, distribution and list how to understand

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

Share

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

C language pointer dynamic, distribution and list how to understand, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

Pointer is a very important concept in c language, and it is also one of the features of c language. Pointers can be used to deal with complex data, control the memory allocation of the computer, and return multiple values in function calls.

Pointers, references, and values

A pointer is a variable that stores the memory address of a computer. Reading data from the memory that the pointer points to is called the value of the pointer. Pointers can point to specific types of variable addresses, such as int, long, and double. Pointers can also be void types, NULL pointers, and uninitialized pointers.

Pointers and arrays

An array represents a contiguous piece of memory space used to store multiple objects of a particular type. In contrast, pointers are used to store a single memory address. Arrays and pointers are not the same structure and therefore cannot be converted to each other.

An array variable is a constant. You cannot assign a pointer to an array variable even if it points to the same address or to a different array. Nor can you assign one array variable to another array. However, it seems puzzling that an array variable can be assigned to a pointer. When you assign an array variable to a pointer, you actually assign the address to the first element of the array to the pointer.

Pointer and structure

Just like an array, the pointer to the structure stores the memory address of the first element of the structure. Like array pointers, the pointer to a structure must be declared to be consistent with the structure type, or declared as a void type.

Need to pay attention to two different symbols,'.' And'- >'. Structure instances can be done by using'.' Symbol accesses the age variable. For pointers to structure instances, we can access the name variable through the'- > 'symbol, or we can also access the name variable through (* ptr) .name.

Dynamic memory allocation

The so-called dynamic memory allocation refers to the method of dynamically allocating or reclaiming the allocated memory of storage space during the execution of the program. Unlike static memory allocation methods such as arrays, dynamic memory allocation requires pre-allocation of storage space, but is allocated immediately by the system according to the needs of the program, and the size of the allocation is the size required by the program.

Dynamic allocation: the program calls the malloc library function to apply for allocation during execution.

Dynamic memory allocation can flexibly deal with unknown purposes. Dynamic objects are unnamed variables that need to be manipulated indirectly through pointers. The allocation and release of dynamic objects must be explicitly managed by the programmer through the malloc () and free () functions (new and delete operators in C++).

The following is an example of dynamic allocation:

P1 = (char *) malloc (10*sizeof (int))

1.malloc function

The prototype of malloc function is: void * malloc (unsigned int size)

Its function is to allocate a contiguous space of length size in the dynamic storage area of memory. Its parameter is an unsigned × × number, and the return value is a pointer to the starting address of the allocated continuous storage domain. It is also important to note that a NULL pointer is returned when the function fails to allocate storage space (such as insufficient memory). So when calling this function, you should detect whether the return value is NULL and perform the appropriate operation.

The following example is a program for dynamic allocation:

# include

# include

# include

# include

# include

Main ()

{

Int count,*array; / * count is a counter, and array is an integer pointer, which can also be understood as pointing to the first address of an integer array * /

If ((array= (int *) malloc (10*sizeof (int) = = NULL)

{

Printf ("storage space could not be allocated successfully.")

Exit (1)

}

For (count=0;countlink=q), which completes the insert operation.

The insert command requires a malloc call to get a new unit from the system and perform pointer adjustments twice thereafter.

3. Delete

If we already know the location of the node p to be deleted, to delete the p node, we only need to make the link domain of the precursor node of the p node be the address of the subsequent node of the p node from the address of the stored p node, and recover the p node.

After reading the above, have you mastered how to understand the dynamics, distribution and list of c language pointers? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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