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

What are the relevant knowledge points of C language pointers?

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

Share

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

This article will explain in detail what are the relevant knowledge points about the C language pointer, the editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

The first stop character pointer-the pointer that stores the character address. Usage

(1) use characters directly

Int main () {char ch = 'wicked; char * pc = & ch; * pc =' wicked; return 0;}

(2) use "" for strings

Int main () {const char* pstr = "hello bit."; / / does this put a string in the pstr pointer variable? Printf ("% s\ n", pstr); return 0;}

We know that the size of a pointer variable is 4byte, so obviously it's impossible to hold an entire string here.

It is actually the address of'h 'that is stored in ptr.

It is somewhat similar to the array name, which generally represents the address of the first element of the array rather than the entire array.

To deepen our understanding of him, let's take a look at the following code

# include int main () {char str1 [] = "hello bit."; char str2 [] = "hello bit."; const char * str3 = "hello bit."; const char * str4 = "hello bit."; if (str1 = = str2) 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;}

As a result,

Arrays are stored in memory by pressing the stack, so we need to be careful to avoid stack overflows when using arrays. While "abcedf" is a constant string, it is stored in the static area of memory. Str3 and str4 point to the same space and are naturally equal.

This is the end of the introduction of the character pointer for the time being. I look forward to seeing it next time.

two。 Pointer array-array of pointers int* arr1 [10]; / array of shaping pointers / array elements 10 int*char * arr2 [4]; / array of first-level character pointers 4 char*char * * arr3 [5]; / / array of secondary character pointers / / array elements 5 char*

To determine what the pointer points to, I have a good way to share it with you.

Get rid of the variable name, that is, get the variable type

3. Array pointer 3.1 definition of array pointer

Is the array pointer a pointer? Or an array?

The answer is: pointer.

We are already familiar with:

Shaping pointer: int * pint; can point to a pointer to shaping data.

Floating-point pointers: float * pf; pointers to floating-point data.

The array pointer should be a pointer that can point to the array.

Which of the following code is the array pointer?

What are int * p1 [10]; int (* p2) [10]; / / p1, p2, respectively?

Explanation:

P2 is combined with * to show that p2 is a pointer variable, and then points to an array of 10 integers. So p is a pointer to an array called an array pointer.

The combination of p1 and [] shows that p1 is an array, and the array elements are 10 elements of int* type.

Note that [] has a higher priority than *

3.2Array name VS Array name

For the following array:

What are arr and & arr respectively?

We know that arr is the array name, which represents the address of the first element of the array.

So-what exactly is the name of the arr array?

Let's look at a piece of code:

# include int main () {int arr [10] = {0}; printf ("% p\ n", arr); printf ("% p\ n", & arr); return 0;}

The result is that both have the same address.

It can be seen that the array name and & the address printed by the array name are the same. Are the two the same? Let's take a look at the following code

# include int main () {int arr [10] = {0}; printf ("arr=% p\ n", arr); printf ("& arr=% p\ n", & arr); printf ("arr+1=% p\ n", arr+1); printf ("& arr+1=% p\ n", & arr+1); return 0;}

We found that although arr and & arr have the same address, their + 1 results are quite different.

Why?

In fact:

& arr represents the address of the array, not the address of the first element of the array. In this example, the type of & arr is: int (*) [10], which is the address of an array pointer type array + 1, skipping the size of the entire array, so the difference between & arr+1 and & arr is 40.

3.3 use of array pointers

How do you use array pointers? Since the array pointer points to the array, the address of the array should be stored in the array pointer.

Look at the code:

The use of an array pointer:

# include int main () {int arr [10] = {1 int arr [10] = {1, 2, 3, 5, 5, 7, 8, 9, 0}; int (* p) [10] = & arr;// assigns the address of the array arr to the array pointer variable p / /, but we seldom write the code return 0;}

The use of an array pointer:

# include// uses the following table to print the array void print_arr1 (int arr [3] [5], int row, int col) {int I = 0 row; int j = 0; for (I = 0 for (j = 0; j < col; jacks +) {printf ("% d", arr [I] [j])) } printf ("\ n");}} / / print the array void print_arr2 (int (* arr) [5], int row, int col) {int I = 0; int j = 0; for (I = 0; I < row; iTunes +) {for (j = 0; j < col) with pointer Printf +) {printf ("% d", arr [I] [j]);} printf ("\ n");}} int main () {int arr [3] [5] = {1 int arr [3] [5] = {1 arr 2] [5] = 3, 5, 6, 7, 8, 9 10}; print_arr1 (3, 5) / / the name of the array arr, indicating the address of the first element / / but the first element of the two-dimensional array is the first row of the two-dimensional array / / so the arr passed here is actually equivalent to the address of the first row. It is the address of the one-dimensional array / / the address of the array pointer can receive print_arr2 (arr, 3,5); return 0 } this is the end of this article on "what are the relevant knowledge points of C language pointers?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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