In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to use pointers in C language. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Pointer summary
Some of the notes come from Wang Dao C language training Camp.
Pointer: address of variable
Pointer variable: the address of a variable used specifically to store another variable.
1. The essence of pointers 1.1 definition of pointers
Access the variable directly by taking the address (pointer)
Access another variable indirectly through a pointer variable
1.2 take address operator and take value operator
Note:
(1) the ∗ * ∗ before the pointer variable indicates that the variable is a pointer variable.
Int* paired data; / / the variable name is p, which is the pointer type (int*) to integer data.
(2) the type of pointer variable must be specified when defining it.
Float a dominating / floating-point variable int* paired variables; / / error, pointer to integer variables
(3) mixed use of address operator and value operator (both operators have the same priority and should be combined from right to left)
Float* gets the value of variable a by dereference, and then takes the storage address of the value, which is equivalent to & a; / / get the address of variable a first, and then dereference to get the value of a, which is equivalent to a.
(4) define multiple pointer variables continuously.
Int* a _ rec; / / error, here only an is the pointer of an integer variable, and b _ ~ c is an integer variable int* a _ r _ b _ r _ c; / / correct 2. Use of pointers scenario 2.1 delivery of pointers
Value transfer
Click to enter the body of change function sentence by sentence
Click on the process after entering the function body
In order to solve the above problems, pointer passing is introduced.
2.2 offset of pointer (addition or subtraction of pointer)
2.3 pointers and self-increment and self-subtraction operators
∗ * ∗ and + + + have the same priority, and the mixed use is from right to left.
The first step in analysis: first remove, then increase or decrease.
The second step in the analysis: whether the priority of the preceding symbol is higher than the priority of increment / subtraction, if so, execute the front first, otherwise execute the latter first.
It's equivalent to the same thing as jacks, packs,
J = (* p) + +; equivalent to jroomroomp; (* p) + +
Int a [3] = {2rec 7pm 8}; int* ptincusa Tincinca raceme p [0] is equivalent to * pj=p [0] + +; / equivalent to Jacup [0]; p [0] + +
2.4 pointers and one-dimensional arrays
The essence of a function call is value transfer (arguments are assigned to formal parameters)
The array name is weakened to a pointer in the process of passing
Pointer and dynamic memory request (malloc)
The array length of C language is fixed because its defined integer, floating point, and character variables are all in the stack space, and the size of the stack space is determined at compile time. If the size of the space used is uncertain, use heap space
A program is an ordered set of instructions placed on disk
A program is called a process only when it is started.
# includevoid * malloc (size_t size); / / malloc releases / / void* as an untyped pointer when applying for space from the heap, and does not specify what type of variable the pointer points to / / malloc does not know what type of data our applied space is used to store. / / so after determining what type to store, the void* will be cast to the corresponding type.
Application heap space
Release the requested heap space
# includevoid free (void * ptr)
Free (p); pendant null; / / if p is not set to NULL, we call p the difference between wild pointer stack space and heap space
After the function stack space is released, all local variables in the function disappear.
Stack space is released with the end of the function
Heap space will not be released at the end of function execution
Initialization of char* p = "hello"; / / character pointer, assigning the first address of the string constant "hello" to pchar c [10] = "hello"; / / character array initialization, equivalent to strcpy (c, "hello")
3. Secondary pointer
The second-level pointer only serves the transmission and offset of the first-level pointer.
3.1 transfer of secondary pointers
To change the value of a variable in a subfunction, the address of the variable must be passed in
To change the value of a pointer variable in a subfunction, you must pass in the address of the pointer variable
The initialization of int* pointer must be the address of a first-level pointer. & p is a second-level pointer type.
Example of a secondary pointer:
Example of first-level pointer:
This is the end of this article on "how to use pointers in C language". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.