C Language—Daily Multiple Choice Questions—Day45

first question

1. Among the following options, the operator that cannot operate on pointer variables of the same basic type is ()

A:+

B:-

C:=

D:==

Answer and analysis A

A: Error, pointers cannot be added, because pointer addition may go out of bounds, so it is not allowed;

B: Correct, pointer subtraction is used to find the number of data of the same data type inside.

C: Pointers are allowed to be assigned

D: Determine the equality of pointers, that is, if the addresses are different, if the addresses are the same, the variables inside are also the same;

Question 2

2. The output result of the following program segment is ( )

int a, b;
b = (a = 3*5, a*4, a*5);
printf("%d",b);

A:60

B:75

C:65

D: No definite value

Answer and analysis B

This question examines a comma expression. The comma expression calculates the formulas between the commas in sequence, but the result of the comma expression is the result of the last formula inside;

So the results of (a = 3*5, a * 4, a * 5) are (1, 60, 75) respectively

Remember that a has been assigned a value in the first expression, so subsequent a will have this value.

The final answer is 75

Question 3

3. The following expression has the same result as ++*p ()

int a[]= {1,2,3,4,5};
int *p = a;

A:*++p

B:a[0]

C:a[0]++

D:*p++

Answer and analysis A

This question tests the usage of priorities and pointers

Although the priority of prefix ++ is higher than *, the associativity of prefix ++ is from right to left. The value of the left operand must be calculated first and cannot exceed *, so *p = 1 is calculated first; Then prepend ++, and finally ++*p = 2;

Related blogs:C language operator priority table (recommended to collect and read it every time) - CSDN Blog

A: *++p, the precedence of ++ is higher than *, and the precedence of ++ returns the value after ++, so *++p is equivalent to *(p + 1) = 2;

B:a[ 0 ] = 1

C: a[ 0 ]++, followed by ++, returns the value before ++, so the value of a[ 0 ]++ expression is 1;

D: *p++, postfixed ++ has a higher priority than *, but postfixed ++ returns the current value, so the address of p in this expression is still a, so it is equivalent to *p, *p = * a = 1;

Question 4

4. int *p[4] is equivalent to () in the selection

A:int p[4]

B:int *p

C:int *(p[4])

D:int (*p)[4]

Answer and analysis C

First of all, the [ ] of int *p [4] in the question has a priority higher than * 

So p is first combined with [4] to form an array. Each element in the array is of type int *, which is a pointer array;

A: int p[4], integer array;

B: int *p, integer pointer;

C: int * (p[4]) is enclosed in parentheses and is an array of pointers;

D: int (*p) [4] This is a pointer, pointing to an array, and is an array pointer;

Question 5 (detailed review!!!!!)

5. Assume that the function prototype and variable description are as follows, and the illegal call is ()

void f3(int(*p)[4]);
int a[4]={1,2,3,4},
int b[3][4]={
   
   {1,2,3,4},{5,6,7,8},{9,10,11,12}};

A:f3(&a);

B:f3(b[1]);

C:f3(&b[1]);

D:f3(b);

Answer and analysis B

In fact, when a function passes parameters, the type must match; this question tests those array pointers;

Because int (*p)[4] is a pointer to an array of pointers; and how can a pointer point to the entire array? It must be the address of the stored array, which means that the type of p is actually &array name, &a. p is equivalent to a secondary pointer which is int**, but the difference is that p must also have the number of arrays. match;

A: &a, a is the array name, &a represents the address of the entire array, so it matches the parameter type of f3;

B: b[1], b is a two-dimensional array. The row name of the two-dimensional array is equivalent to the array name of each row. Because the two-dimensional array is actually an array of one-dimensional arrays, then each row is equivalent to a One-dimensional array, isn't the row name the array name of our one-dimensional array, so b[1] is the array name, the array name is the address of the first element, and the first element of the row b[1] is b[1][0 ], the address type is int*, which does not match our p type.

C: &b[1] is correct, it is the address of the entire array;

D: b is the array name, the name of the two-dimensional array, which is the address of the first element of the two-dimensional array. The first element of the two-dimensional array is the entire one-dimensional array, so the data type of b is actually the address of the one-dimensional array, and p type matching; because if you think about it, the first element of a one-dimensional array is a single number, but a two-dimensional array is an array of one-dimensional arrays, so the elements of the two-dimensional array must be one-dimensional arrays one by one!

Guess you like

Origin blog.csdn.net/2302_76941579/article/details/134958212