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 test questions for pointers in C language?

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you what the pointer in the C language has written test questions, I believe that most people do not know much, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

1. Pointer pen test question 1. The title is as follows:

Analyze sentence by sentence:

1. An integer array of size 5 is defined and initialized

two。 An integer pointer variable ptr is defined to store the address. The storage address here is the address of (& a). Because it is & a, it represents the entire array a, so & aqum1 skips the whole array, and the address here is the address of a random variable behind the array A. Because the pointer points to the entire array, its type is int (*) [5] (array pointer), so we cast it in front of it, convert it to type (int*), and then store it inside the pointer variable ptr.

3. Print out the corresponding values of * (aql) and * (ptr-1)

Illustration:

Running result:

two。 The title is as follows:

Analyze sentence by sentence:

1. Define a structure variable for us to use later

② .p is an address, so you can just add and subtract it with hexadecimal. 0x1 is hexadecimal 1, so it can be understood as pair1. The corresponding hexadecimal address is printed. Because the size of the structure is 20 bytes, + 1 is to skip the whole structure, that is, to add the size of the whole structure. That is, the address of the corresponding p + 20 gets the new address printed out, and the 20 added here is decimal, and we need to convert it to a hexadecimal number, that is, the address of p plus 0x00000014.

③ .p does an unsigned long integer cast here, and then adds 0x1 (that is, decimal 1). We think that since we convert p type to an integer type, + 1 is simply + 1. Let's take a simple example. 10 and 1 are integer data. 10 is stored in memory is 0x0000000A, and the memory storage is 0x00000001. the corresponding memory storage is 0x0000000B. So we know that the two integers add together, and the corresponding address is + 1.

④ .p does a forced type conversion of an unsigned integer pointer here, and then adds 0x1 (that is, decimal 1). We think that since we convert the p type to an integer pointer, we add + 1 and skip an integer pointer variable, and the size of an integer pointer variable is 4, so the address corresponding to + 1 is + 4.

Running result:

3. The title is as follows:

Analyze sentence by sentence:

1. An integer array of size 4 is defined and initialized

two。 Defining an integer pointer variable ptr1 stores the address of the variable, which is consistent with the method of the first question. I will not repeat it here, just look up at the first question.

3. Here an is placed separately in parentheses, representing the address of the first element of the array, and the type is (int*). After a step in front of a forced type conversion converts int* type to integer (int type) plus + 1, we can know from the previous question that the integer value + 1 address is also + 1 byte, and then forced type conversion to convert it to int* type, storing the address in the pointer variable ptr2

4. Note that% x is printed in hexadecimal ptr [- 1] and * ptr2

Illustration:

Running result:

4. The title is as follows:

Analyze sentence by sentence:

1. A two-dimensional array of three rows and two columns is defined and initialized, and we see that the elements in it are all enclosed in parentheses, so what we see here is a comma expression, and we know that the value of the comma expression is taken from the last element, so it is equivalent to that our array elements are all 0 if they are not initialized in other positions. So that's what our array is like, int a [3] [2] = {1pr 3pm 5pm 0pm 0pm 0}.

two。 Define a pointer variable p

3. The pointer variable p is used to store the address of a [0]. We notice that the a [0] here has neither & a [0] nor sizeof (a [0]), so the a [0] here is the address of the first element of the array, that is, the address of the number 1, so the pointer variable p stores the address of the a [0] [0] element.

4. Print out the element value corresponding to p [0], and we know that p [0] = * (pendant 0), so it is the value corresponding to a [0] [0].

Illustration:

Running result:

5. The title is as follows:

Analyze sentence by sentence:

1. Define a two-dimensional array.

two。 Define an array pointer.

3. Assign the first address of the two-dimensional array to the array pointer, we see that the type here is int (*) [4], while the two-dimensional array obviously has five columns, so obviously this array pointer can not store the whole two-dimensional array, but can only store part of it.

4. Print out the address corresponding to the difference of the address, and the corresponding difference value.

Illustration:

Running result:

6. The title is as follows:

Analyze sentence by sentence:

1. Define a two-dimensional array and initialize it

two。 Defining a pointer variable ptr1 stores the address of the variable. We see that the array name here is represented by &. We know that & the array name represents the entire array, so & aa+1 skips the entire array, because the corresponding type of the entire array is int (*) [5], so we need to do an one-step cast to convert it to the (int*) type and store it in the pointer type variable ptr2.

3. The aa here does not have a & symbol, and it is not inside the sizeof, so the aa here is the address of the first element. Because it is a two-dimensional array, the address of the first element is the address of the first line, and then jumps to the second line after + 1.

4. Print out the values of * (ptr1-1) and * (ptr2-1)

Illustration:

Running result:

7. The title is as follows:

Analyze sentence by sentence:

1. Defines an array of character pointers and initializes them

two。 Define a secondary pointer that holds the first address of the element of the character pointer array

3. The secondary pointer + + jumps the address of an element, pointing to the address of the next element.

4. Print out the elements corresponding to * pa

Illustration:

Running result:

8. The title is as follows:

Analyze sentence by sentence:

1. Defines an array of character pointers and initializes the

two。 Defines an array of character pointers (secondary pointers) and initializes the

3. Define a tertiary pointer to an array of cp character pointers

④ ~ ⑦ to print values as required

Illustration:

Running result:

The above is all the contents of the article "what are the written test questions in C language?" Thank you for your 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