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

The specific usage of C language pointer

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

Share

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

This article mainly explains "the specific usage of C language pointer". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. let's study and learn the specific usage of C language pointer together.

Catalogue

1. Character pointer

two。 Pointer array

3. Array pointer

4. Function pointer

5. Array parameter transfer

Summary

1. Character pointer

We already know that the array name represents the address of the array most of the time, and the pointer essentially represents an address, so can we use the pointer to create a string?

Int main () {char arr1 [] = "abcdef"; char arr2 [] = "abcdef"; const char* p1 = "abcdef"; const char* p2 = "abcdef"; / / const can be omitted and defaults to the constant string printf ("% s\ n", arr1); printf ("% s\ n", arr2); printf ("% s\ n", p1) Printf ("% s\ n", p2); return 0;}

It should be noted that the string created by the character pointer is a constant string, an ordinary string is a different string as long as the array name is different, even if the string content is the same, but no matter how many pointers are used to create the string, as long as the string content is the same, all pointers point to the same string, so the string pointed to by the character pointer cannot be modified, even without const decoration.

two。 Pointer array

We know that arrays can store shaping, floating-point, or custom structures, so can pointers be stored? The answer is: yes. Elements are pointers of the array is called pointer array, in the form of: data type * array name [], for example: store the shaping pointer array created into int*p [10], represents an array containing ten pointer elements, using the pointer array we can access the pointer array and then manipulate variables or other arrays.

Int main () / traverses three arrays {int arr1 [] = {1,2,3,4,5}; int arr2 [] = {2,3,4,5,6}; int arr3 [] = {3,4,5,6,7}; int*parr [] = {arr1, arr2, arr3}; int I, j; int Sz = sizeof (parr) / sizeof (parr [0]) Int sz = sizeof (arr1) / sizeof (arr1 [0]); for (I = 0; I < Sz; iTunes +) {for (j = 0; j < sz; jacks +) {printf ("% d", * (parr [I] + j));} printf ("\ n") }} 3. Array pointer

We know that pointers can point to shaping, floating-point, or custom structures, so can they point to arrays? The answer is also: yes. A pointer to an array is called an array pointer in the form of a data type (* p) [], such as int (* p) [10], which represents a pointer to an array of ten elements, which differs from an array of pointers by enclosing * p in parentheses to ensure that it is a pointer rather than an array (p is combined with [] without parentheses).

Int main () uses the array pointer to traverse the array {int arr [6] = {1, 2, 3, 4, 5, 6}; int (* p) [6] = & arr; int i; int sz = sizeof (arr) / sizeof (arr [0]); for Ip [10] is an array of pointers, the remaining int (*) [5] is the type of array, the type is array pointer, p is an array of ten elements, each element is an array pointer, pointing to an array with five elements of type int return 0;}

With pointer array and array pointer, we can also nest the two, for example: array pointer array (int (* p []) []), pointer array pointer (int* (* p) []), the former is an array where elements in an array are pointers to other arrays, and the latter is an array whose elements are all pointers to other arrays.

Void print (int (* p) [5], int xpenint y) / / receives the array address {int iRecine j with the array pointer; for (I = 0; I < x; icontest +) {for (j = 0; j < y) {printf ("% d", * (p + I) + j)) / / arr [I] = * (arr+i) = * (pyri) = = p [I]} printf ("\ n");}} int main () / / traversing the two-dimensional array {int arr [3] [5] = {{1,2,3,4,5}, {2,4,5,6}, {3,4,6,7}} using array pointers Print (arr, 3,5); / / arr is the address of the first element, and the first element of the two-dimensional array is an one-dimensional array of the first row return 0;} 4. Function pointer

We know that pointers can point to shaping, floating-point types, custom structures and arrays, so can they point to a function? The answer is also: yes. The function pointer is in the form of: function return type (* p) (parameter type, parameter type.) for example: int (* p) (int,int) represents a pointer to a function whose return type is integer, and the two parameter types are also shaped. Note: there are several dereferencing operators when calling a function with a function pointer, but parentheses are indispensable!

Int Mul (int x, int y) {return x;} int main () {int a, b; scanf ("% d% d", & a, & b); int (* p) (int,int) = & Mul;// function pointer, with or without & can printf ("% d", (p) (a, b)) / call function with pointer (* (void (*) () 0)) () printf ("% d", (* p) (a, b)); printf ("% d", (* * p) (a, b)); printf ("% d", (* p) (a, b)); / / any number of * can return 0;} 5. Array parameter transfer

We know a simple way to pass parameters to an one-dimensional array: the array name is the address of the first element, and we can receive it directly with a pointer or an array. What about a pointer array and a two-dimensional array? The pointer array also passes the address of its first element, and its first element is a pointer. Of course, we use the secondary pointer to receive the address of the pointer. Similarly, we can also receive the address of the pointer array directly. What a two-dimensional array passes is the address of its first element, it should be noted that the first element of a two-dimensional array is not the first element but an one-dimensional array composed of the first row of elements. of course, we use the array pointer to receive the address of the one-dimensional array, similarly, we can also receive it directly with a two-dimensional array.

Void test1 (int arr []) / / Direct array receive {printf ("% d\ n", arr []);} void test2 (int*arr) / / pointer receive first element address {printf ("% d\ n", arr []);} void test3 (int arr [] [5]) / / binary array direct receive {printf ("% d\ n", arr [] [5])) } void test4 (int (* arr) [5]) / Array pointer receive {printf ("% d\ n", arr);} void test5 (int*arr []) / / Direct pointer Array receive {printf ("% d\ n", arr);} void test6 (int**arr) / / Secondary pointer receive {printf ("% d\ n", arr) } int main () {int arr1 [5] = {1pje 2je 3je 4je 5}; int arr2 [3] [5] = {1pje 2je 3je 4je 5}; int* arr3 [5] = {NULL}; test1 (arr1); test2 (arr1); test3 (arr2); test4 (arr2); test5 (arr3); test6 (arr3); return 0 } Thank you for your reading. this is the content of "the specific usage of C language pointer". After the study of this article, I believe you have a deeper understanding of the specific usage of C language pointer. The specific use of the situation also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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