In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "how to use C language pointer". The content is simple and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn this article "how to use C language pointer".
Character pointer
Among the types of pointers, we know that there is a pointer type called character pointer char*.
Consider the following code, where does pc and p point, respectively?
Int main () {char ch = 'www'; char* pc = & ch;//pc is const char* p = "hello boy" pointing to a character variable; / / "hello boy" is a constant string / / the above expression is used to assign the address of the first character h of the constant string "hello bit" to p (pointing to the first character address) return 0;}
[note]
The code char* pstr = "hello bit."; it's easy for students to think that they are putting the string hello boy on the character pointer.
P, but / essentially puts the address of the first character of the string hello boy in p.
Think about the following code, what is the output?
Int main () {char str1 [] = "hello boy."; char str2 [] = "hello boy."; / / two character arrays, opening up space independently. / / the array name is the address of the first element of the array char * str3 = "hello boy."; char * str4 = "hello boy." / / both point to constant strings, (constant strings cannot be modified) / / both point to the same address if (str1 = = str2) / / compare the addresses of two arrays, definitely not equal to printf ("str1 and str2 are same\ n"); else printf ("str1 and str2 are not same\ n") If (str3 = = str4) printf ("str3 and str4 are same\ n"); else printf ("str3 and str4 are not same\ n"); return 0;}
So the result is as follows
Pointers array and array pointers
Is the pointer array a pointer or an array?
The answer is: array.
Are array pointers pointers or arrays?
The answer is: pointer.
For example:
Int * p1 [5]; / / pointer array int (* p2) [5]; / / array pointer
The forms of the two are very similar, so how can we distinguish them?
1. Pointer array
[pointer array]
First of all, it is an array, the elements of the array are pointers, and the number of bytes of the array is determined by the array itself. It is the abbreviation of "array of pointers".
An array of pointers is an array whose elements are pointers (for example, int * p [5], which defines five pointers p [0], p [1], p [2], p [3], p [4]).
Int * p [5]
A priority issue is involved here.
We know that the array subscript takes precedence over the value operator. Therefore, p is first defined as an array with five elements. Its type is int*, so it is a pointer to an integer variable.
[conclusion] the pointer array is an array, and each array element stores a pointer variable.
How is the pointer array initialized?
Int main () {/ / char * arr [5]; / / arr is an array of character pointers / / int * arr2 [4]; / / arr2 is an array of integer pointers int a = 10; int b = 20; int c = 30; int d = 40 / / int* arr2 [4] = {& a, & b, & c, & d}; / / arr2 is the array of integer pointers printf ("% d\ n", * arr2 [0]); / / fetch the contents of the first address int I = 0; for (I = 0; I)
< 4; i++) { printf("%d\n", *(arr2[i])); } return 0;} 数组指针中&a,&b,&c,&d分别指向10,20,30,40Have you found that if you define it in this way, it will be a bit cumbersome.
So we can use the following methods:
Int main () {const char* arr [5] = {"abcedf", "bcedfg", "hehe", "hhh", "zhangsan"}; int I = 0; for (I = 0; I < 5; iTunes +) {printf ("% s\ n", arr [I]);} return 0;}
two。 Array pointer 2.1. What is the array pointer?
[array pointer]
First of all, it is a pointer that points to an array. It always takes up 4 bytes in a 32-bit system.
As for the number of bytes it points to, I don't know. It is short for "pointer to array".
An array pointer is a pointer to the address of an array, which is essentially a pointer.
Int (* p) [5]
In the above code, parentheses and array subscripts are in the same priority queue, so execute from left to right.
Therefore, p is first defined as a pointer variable, followed by [5] represents an array of five elements, and p points to this array.
Because the type of the pointer variable is actually the type of the element it points to, the int defines the type of the array element as an integer.
Use the following example to deepen your understanding
Int main () {int a = 10; the address of the int*pi=&a;// integer is stored in the integer pointer char ch = 'wicked; the address of the char* pc=&ch;// character is stored in the character pointer int arr [10] = {0}; int*p = arr;//arr- is the address of the first element of the array / / int* parr [10] / / write the array int (* parr) [10] = & arr;// takes out the address of the array and should store it in the array pointer return 0;}
So how do we initialize it?
When we learn pointers, we point the pointer to the array name, because the array name is the address of the first element of the array. If you know the address of the first element, you can know the following elements. As follows:
Int main () {int arr [] = {1,2,3,4,5}; int * p = arr; int i = 0; for (I = 0; I < 5; iTunes +) {printf ("% d\ n", * (p + I));} return 0;}
So, the pointer p above is a pointer to an integer variable, not a pointer to an array. The array pointer is the pointer to the array.
Therefore, during initialization, you should pass the address of the array to the array pointer, not the address of the first element of the array. Although their values are the same, they have different meanings.
Int main () {int arr [] = {1,2,3,4,5}; int (* p) [] = & arr; int i = 0; for (I = 0; I < 5; iTunes +) {printf ("% d\ n", * (* p + I));} return 0;}
2.2.& the difference between array name and array name
Let's take arr and & arr as examples:
The values of "a" and "a" are the same.
But the meaning is different.
An is the first address of the first element of the array, that is, the first address of a [0].
& an is the first address of the array and represents the address of the array.
For example:
Int main () {int arr [5] = {0}; printf ("% p\ n", arr); printf ("% p\ n", & arr); return 0;}
As you can see, their values are the same.
But what if they are + 1?
As follows:
# include int main () {int arr [5] = {0}; printf ("arr=% p\ n", arr); printf ("& arr=% p\ n", & arr); / / + 1 look at printf ("arr+1=% p\ n", arr+1); printf ("& arr+1=% p\ n", & arr+1); return 0;}
As you can see, the result after + 1 is different.
So why?
An is the first address of the first element of the array, that is, the first address of a [0].
& an is the first address of the array.
Aroom1 is the first address of the next element of the array, that is, the first address of a [1].
& astat1 is the first address of the next array.
2.3. The use of array pointers
The array pointer points to the array and stores the address of the array
How to use it, for example:
# include int main () {int arr [10] = {0}; int (* p) [10] = & arr;// assigns the address of the array arr to the array pointer variable p / / here * combines first with p and then with []. Because the array defined above is of type int, the type of address is also type int. Return 0;}
Take a look at the following code and think about how we can use array pointers to print the results we want.
Void print (int (* parr) [10], int sz) / / pass the address and accept {int I = 0; for (I = 0; I < sz; iTunes +) {/ / the following three ways to print / / printf ("% d", parr [0] [I]) / / treat an one-dimensional array as a two-dimensional array, [0] represents the first row, [I] represents the traversal element / / printf ("% d", (* (parr + 0)) [I]); / / * (parr + 0) dereferencing the address of the first element printf ("% d", (* parr) [I]) / / (* parr) is equivalent to the array name of the array pointed to by parr} int main () {int arr [10] = {1, 2, 3, 4, 5, 6, 8, 9, 10}; int sz = sizeof (arr) / sizeof (arr [0]); print (& arr, sz); / / & arr passes the address of the first element of the array to the return 0;}
Array parameters and pointer parameters
We all know that parameters are divided into formal parameters and actual parameters.
Formal parameters are parameters when a function is declared or defined.
The argument is the actual value passed by the tone function when the function is called.
1. One-dimensional array parameter
What is it like to pass parameters in an one-dimensional array?
Let's first look at an example:
Please think about whether the following parameters can be passed successfully.
# include void test (int arr []) / / ok? {} void test (int arr [10]) / / ok? {} void test (int * arr) / / ok? {} void test2 (int * arr [20]) / / ok? {} void test2 (int * * arr) / / ok? {} int main () {int arr [10] = {0}; int * arr2 [20] = {0}; test (arr); test2 (arr2);}
two。 Two-dimensional array parameters
The parameter passing of a two-dimensional array is similar to an one-dimensional array.
For example:
Also think about whether the transmission of parameters can be successful?
Void test (int arr [3] [5]) / / ok? {} void test (int arr [] []) / / ok? {} void test (int arr [] [5]) / / ok? {} / Summary: two-dimensional array parameters, the design of function parameters can only omit the number of the first []. / / because for a two-dimensional array, you don't know how many rows there are, but you must know how many elements a row has. / / this is the only way to facilitate calculation. Void test (int * arr) / / ok? {} void test (int* arr [5]) / / ok? {} void test (int (* arr) [5]) / / ok? {} void test (int * * arr) / / ok? {} int main () {int arr [3] [5] = {0}; test (arr);}
3. First-order pointer parameter transfer
First of all, pass the parameter with the first-order pointer, then receive it with the first-order pointer.
# include void print (int* p, int sz) / / first-level pointer receives int* p {int iTuno; 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.
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.