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 apply C language array pointer and pointer array

2025-03-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to apply C language array pointers and pointer arrays". In daily operation, I believe that many people have doubts about the application of C language array pointers and pointer arrays. I have consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about how to use C language array pointers and pointer arrays. Next, please follow the editor to study!

I. Array type

Arrays in C language have their own specific types.

The type of the array is determined by both the element type and the array size

The type of int array [5] is int [5].

Second, define data types

Renaming array types through typedef in C language: typedef type (name) [size]

Array type:

Typedef int (AINT5) [5]

Typedef float (AFLOAT10) [10]

Array definition:

AINT5 iArray

AFLOAT10 fArray

Array pointer

Array pointer is used to point to an array

The array name is the starting address of the first element of the array, but not the starting address of the array

The starting address of the array can be obtained by taking the address character & acting on the array name

The array pointer can be defined by the array type: ArrayType* pointer

You can also directly define: type (* pointer) [n]

Pointer is the name of the array pointer variable, type is the element type of the pointing array, and n is the size of the pointing array.

Let's look at an example of an array pointer:

# include typedef int (AINT5) [5]; typedef float (AFLOAT10) [10]; typedef char (ACHAR9) [9]; int main () {AINT5 A1; float fArray [10]; AFLOAT10* pf = & fArray; ACHAR9 cArray; char (* pc) [9] = & cArray; char (* pcw) [4] = cArray; int i = 0; printf ("% d,% d\ n", sizeof (AINT5), sizeof (A1)) For (I = 0; I)

< 10; i++) { (*pf)[i] = i; // ==>

FArray [I] = I;} for (I = 0; I < 10; iTunes +) {printf ("% f\ n", fArray [I]);} printf ("% p,% p,% p\ n", & cArray, pc + 1, pcw + 1); return 0;}

The output is as follows:

Note that char (* pcw) [4] = cArray; is wrong, the address represented by the cArray array name is the address of the first element, the type is char*, and the pcw pointer type is char [4], so this is illegal.

Fourth, pointer array

A pointer array is an ordinary array.

Each element in the pointer array is a pointer

Definition of array: type* pArray [n]

Type* is the type of each element in the array, pArray is the array name, and n is the array size

For example:

Let's look at the application of a pointer array:

# include # include # define DIM (a) (sizeof (a) / sizeof (* a)) int lookup_keyword (const char* key, const char* table [], const int size) / / const char* table [] const char** table {int ret =-1; int I = 0; for (I = 0; I < size; ionization +) {if (strcmp (key, table [I]) = = 0) {ret = I Break;} return ret;} int main () {const char* keyword [] = {"do", "for", "if", "register", "return", "switch", "while", "case", "static"} Printf ("% d\ n", lookup_keyword ("return", keyword, DIM (keyword)); printf ("% d\ n", lookup_keyword ("main", keyword, DIM (keyword); return 0;}

The output is as follows:

Note that const char* table [] const char** table can be written either way, but const char* table [] is more intuitive.

At this point, the study of "how to apply C language array pointers and pointer arrays" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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