Pointer related blogs
The first shot of pointer: pointer family-CSDN Blog
In-depth understanding: dereference and addition of pointer variables-CSDN Blog
first question
1. There is the following program, the output result is ()
#include <stdio.h>
int main()
{
char a = 'H';
a = (a > 'A' && a <= 'Z') ? (a + 32) : a;
printf("%c\n", a);
return 0;
}
A:H
B:h
C:A
D:a
Answer and analysis B
This question tests the understanding of ASCII values and conditional expressions
Conditional expression: x? y:z; if x is true, execute y, otherwise execute z;
Obviously this question is true, the execution is a + 32, which is the operation of converting uppercase characters to lowercase letters
Below is the ASCII table:
Question 2
2. Which of the four variables defined is not a pointer type?
#define INT_PTR int*
typedef int*int_ptr;
INT_PTR a,b;
int_ptr c,d;
A:a
B:b
C:c
D:d
E; are all pointers
F: None of them are pointers
Answer and analysis B
Remember #define is replacement, so
INT_PTR a,b = int* a, b, replacement means only one is given, not both by default;
Typedef is a renaming of the type, so int_ptr is int*, both pointer types;
Question 3
3. The result of running the following program is ()
#include <stdio.h>
int f(int n)
{
if (n==1)
return 1;
else
return (f(n-1)+n*n*n);
}
int main()
{
int s=f(3);
printf("%d\n", s);
return 0;
}
A:8
B:9
C:27
D:36
Answer and analysisD
This question examines a simple function recursion
f(3) = f(2) + 3 * 3 * 3
f(2) = f(1) + 2 * 2 * 2
f(1) = 1
so
f(3) = f(1) + 2 * 2 * 2 + 3 * 3 * 3 = 1 + 2 ^ 3 + 3 ^ 3 = 36
Question 4
4. What will the following code output ()
#include <stdio.h>
int main()
{
int a[4] = {1, 2, 3, 4};
int *ptr = (int *)(&a + 1);
printf("%d", *(ptr - 1));
}
A:1
B:2
C:3
D:4
Answer and analysisD
This question tests the dereference and addition of pointers. Please see this blog: In-depth understanding: Dereference and addition of pointer variables - CSDN Blog
Question 5
5. The output of the following program is ()
#include <stdio.h>
int main()
{
int a[12] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, *p[4], i;
for (i = 0; i < 4; i++)
p[i] = &a[i * 3];
printf("%d\n", p[3][2]);
return 0;
}
A: There is an error in the above program
B:6
C:8
D:12
Answer and analysisD
This question is very interesting. First, there is an integer array with 12 variables, and then a pointer array is defined. This pointer array stores &a[0], &a[3], &a[6], &a[9 respectively. ]These four addresses;
When printing, it is p[3][2]. How to understand this?
Use pointers to understand: p[3] [2] = *(p[3] + 2) This means getting the element p[3] first, which is &a[9]
So p[3] [2] = *(&a[9] + 2); that is, the length of 2 int types after the address of a[9], why is it int?
Look at the fourth question blog, and then you come to &a[11], so in the end it is *&a[11] = a[11] = 12;
In-depth understanding: dereference and addition of pointer variables-CSDN Blog