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

How to use Array pointer and function pointer in C language

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

Share

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

This article mainly shows you "C language how to use array pointers and function pointers", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "C language how to use array pointers and function pointers" this article.

Function pointer syntax

Define a function pointer and call the function indirectly through the function pointer:

Int get_num (int a, int b) {return a + b;} int (* func) (int a, int b); / / defines a function pointer func, which points to the function func = & get_num; / / function pointer to function func (1,2) that returns the parameter int an and int b; / / calls the function through the function pointer

Define a function pointer by defining a function pointer type:

Typedef int (* func) (int a, int b); / / defines a function pointer type func func1 = & get_num; / / defines the function pointer variable func1 using the function pointer type func

Define a function type and use the function type to define a function pointer

Typedef int (func) (int a, int b); / / defines a function type func* func1 = & get_num;func1 (1,1); / / indirect call

The function pointer prescribes the return value of the function and the function parameters. as long as the function developer implements the function according to this convention, the function developer can use the function of this function through a unified interface with the function pointer as parameters. the decoupling between function development and function use is realized. This is the great function of function pointers: function pointers as function parameters.

In addition, the positive call of the function pointer uses the scenario, such as by loading the dynamic library into the program and finding the function entry address in the dynamic library to call the function.

Array pointer and pointer array

An array pointer is a pointer to an array; a pointer array is an array of pointers.

Examples of array pointers

# define _ CRT_SECURE_NO_WARNINGS # include # include # include void PrintArray_int (int* array, int len) {if ((NULL = = array) | | (len < 0) {printf ("err: (NULL = = array) | | (len < 0)\ n"); return;} for (int I = 0; I < len) Printf +) {/ / two methods access array elements / / printf ("% d", array [I]); printf ("% d", * (array + I));} printf ("\ n");} int main () {/ / define an array variable int num_array [10] / / num_array + 1 Mobile sizeof (int) bytes int len = sizeof (num_array) / sizeof (num_array [0]); / / memset (num_array, 0, sizeof (num_array) / sizeof (num_ array [0])); / / typical error, only ten bytes of memory value is 0 memset (num_array, 0, sizeof (num_array)) / / 40 bytes should be set to 0 PrintArray_int (num_array, sizeof (num_array) / sizeof (num_ array [0])); / / define an array type typedef int (array1_t) []; typedef int (array2_t) [10]; array1_t A1 = {1,2,3}; array2_t a2; for (int I = 0; I < 10) Sizeof +) {a2 [I] = I;} PrintArray_int (A1, sizeof (A1) / sizeof (A1 [0])); PrintArray_int (a2,10); / / define an array pointer int (* p_array) [10]; / / p_array + 1 Mobile sizeof (int) * 10 bytes p_array = & num_array For (int I = 0; I < 10; iSum +) {(* p_array) [I] = I + 1;} PrintArray_int (* p_array, 10); / / define a pointer type (array pointer) typedef int (* p_array_t) [10] to an array; p_array_t p1 = & num_array For (int I = 0; I < 10; iTunes +) {(* p1) [I] = I + 2 / / first dereferencing and reverting to the first-level pointer (the array itself can be seen as the first-level pointer / / the array pointer is the pointer to the array, that is, the pointer to the first-level pointer, that is, the second-level pointer)} PrintArray_int ((* p1), 10) / / pointer array const char* p2 [2] = {"aaa", "bbb"}; / / p2 [0] and p2 [1] are both pointers printf ("% s\ n", p2 [0]); printf ("% s\ n", p2 [1]); system ("pause"); return 0 } these are all the contents of the article "how to use array pointers and function pointers in C language". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report