In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article shows you how to understand the initial pointers in C language, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
1. What's the pointer?
Beginners have a question, that is, what is the pointer? To put it simply, the memory unit with its address can be found through it.
The address points to a certain memory space, so the address image is called a pointer.
Int main () {int a = 10; int* pa = & a; return 0;} / pa is used to store the address (pointer), so pa is the pointer variable.
Summary: pointers are variables, variables used to store addresses. Values stored in pointers are treated as addresses.
The address uniquely identifies a piece of space.
The size of the pointer is 4 bytes on 32-bit platforms and 8 bytes on 64-bit platforms.
two。 Pointers and pointer types
We know that variables have different types (integer, floating point, character, etc.). In fact, pointers also have different types.
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
Int* pointer dereferencing access to four bytes
Int main () {char* pc = & a; * pc = 0; return 0;}
Meaning of pointer type 2:
The pointer type determines the step size when the pointer is an integer (skip a few bytes when the pointer is an integer).
Int* pointer + 1 skips four bytes
Char* pointer + 1 skips a byte
Int main () {int a = 10; int* pa = & a; char* pc = & a; printf ("% p\ n", pa); printf ("% p\ n", pc); printf ("% p\ n", pa+1); printf ("% p\ n", pc+1); return 0;} 3. Wild pointer
A wild pointer is the position where the pointer points to is unknown (random, incorrect, and undefined).
3.1 causes of wild pointer
1. Pointer not initialized
Int main () {int* pram / local variable pointer is not initialized, default is random value * p = 20 pm / find a space by using the random value stored in p as address, this space does not belong to our current program / / it causes illegal access, p is the wild pointer return 0;}
two。 Pointer out of bounds access
Int main () {int arr [10] = 0; int I = 0; int* p = arr; for (I = 0; I & values [0];) {*-- vp = 0;}
The above program can also be written like this.
For (vp = & values [N _ VALUES-1]; vp > = & values [0]; vp--) {* vp = 0;}
In fact, the task can be done smoothly on most compilers, but we should avoid writing it because the standard does not guarantee that it is feasible.
Standard regulation
Allows a pointer to an array element to be compared to a pointer to the memory location after the last element of the array, but not to a pointer to the memory location before the first element.
5. Pointers and arrays
Array-A contiguous space with elements of the same type
The array size is related to the element type and the number of elements.
Pointer (variable)-is a variable, put the address
The size of pointer variables is 4 (32bit) / 8 (64bit) byte
The array name is indeed the address of the first element
But there are two exceptions:
1.sizeof (array name)-the array name here is not the address of the first element, but represents the entire array. Here, the size of the entire array is calculated, in units or bytes.
2. & Array name-the array name here is not the address of the first element, but represents the entire array. What you get is the address of the entire array.
Int main () {int arr [10] = {0}; int sz = sizeof (arr); printf ("% d\ n", sz); return 0;} int main () {int arr [10] = {0}; int* p = arr; int i = 0; int sz = sizeof (arr) / sizeof (arr [0]); for (I = 0th I < sz) Printf +) {* (p + I) = I;} for (I = 0 + I < sz;i++) {printf ("% d", * (p + I));} return 0;} 6 Secondary pointer
We all know that a pointer variable is a variable, and if it is a variable, it has an address, so where is the address of the pointer variable stored?
This is what we need to know about the secondary pointer.
Int main () {int a = 10; int* p = & a; int** pp = & p * pp pp is the secondary pointer * * pp = 20; printf ("% d\ n", a); / / a = 20 return 0;} 7. Pointer array
Judging from the name, do you think the pointer array is a pointer or an array?
The answer is an array, an array that holds pointers.
Integer array-the array that holds the integer is the integer array
Character array-the array where the characters are stored is the character array
Pointer array-the array where pointers are stored is the pointer array.
An array of int* integer pointers
An array of char* character pointers
Int main () {int arr [10]; char ch [5]; int* parr [5]; char* pc [6]; return 0;} int main () {int a = 10; int b = 20; int c = 30; int* parr [3] = {& an int I = 0 int I < 3 ) {printf ("% d\ n", * (parr [I]));} return 0;} the above is how to understand the initial pointers in C language. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
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.