Pointer related interview questions

The meaning of array names:
1. sizeof( array name ) , the array name here represents the entire array, calculated is the size of the entire array.
2. & Array name, the array name here represents the entire array, and the address of the entire array is taken out.
3. In addition, all array names represent the address of the first element.
Let’s take a look at some classic interview questions
1
int main()
{
    int a[5] = { 1, 2, 3, 4, 5 };
    int *ptr = (int *)(&a + 1);
    printf( "%d,%d", *(a + 1), *(ptr - 1));
    return 0;
}

We first need to know here(&a + 1) What is - &a is to take out the address of the entire array. Originally, ptr pointed to the address of 1, and then gave it +1 It means skipping this array, that is

(a+1) where a represents the address of the first element, +1 represents the address of the second element, so *(*() is the second element, which is 2; 1+ a

*(ptr-1) is the address pointing to 5, and then dereferenced it is 5; so the answer to this question is 2, 5;

2

int main()
{
    int a[4] = { 1, 2, 3, 4 };
    int *ptr1 = (int *)(&a + 1);
    int *ptr2 = (int *)((int)a + 1);
    printf( "%x,%x", ptr1[-1], *ptr2);
    return 0;
}
  int * ptr1 = ( int * ); In this area &a, display and take-out number set location, ptr1 oriented 1 location, +1 jump, number set immediately oriented 4 back location, immediately1 + a & )(
ptr1[-1] is equivalent to *(ptr1-1), which means it points to 4 and then dereferences it to 4;
  int * ptr2 = ( int * ); where a represents the address of the first element, force the type to be converted to int and then +1, that is, only the 1 in the int type is skipped Bytes, integers are stored in little-endian memory, that is, 1, 2, 3, and 4 should originally be stored in memory in the form of 0x00000001, 0x00000002, 0x00000003, 0x00000004. They are stored in little-endian format1+ a )int )((
, so dereference ptr to *ptr=02000000, print it out in the form of %x and omit the preceding 0, and the result should be 2000000;
The answer to this question is 4,2000000;
3
#include <stdio.h>
int main()
{
    int a[3][2] = { (0, 1), (2, 3), (4, 5) };
    int *p;
    p = a[0];
    printf( "%d", p[0]);
 return 0;
}

a is a two-dimensional array of three rows and two column pairs. Pay attention to the initialization of the two-dimensional array here, int a] < /span>) };, among them is Separated by ",", not "{}", so he only initialized 3 elements, namely 1, 3, 5, and the rest are all 0, that is, this two-dimensional array should be5, 4), (3, 2), (1 , 0 { (=2][3[

 p = a[0 ];a[0]indication is &a[0][0], p[0]indication*(p+0)indication*pindication1;

4

int main()
{
    int a[5][5];
    int(*p)[4];
    p = a;
    printf( "%p,%d\n", &p[4][2] - &a[4][2], &p[4][2] - &a[4][2]);
    return 0;
}

a is of type int(*)[5], p is of type int(*)[4], as shown in the figure

& p [ 4 ][ ], it is one element among others, As shown, the result is -4, the division is %p sum% d format stamp, it is2][4[ a- &] 2

FFFFFFFC和-4

5

int main()
{
    int aa[2][5] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    int *ptr1 = (int *)(&aa + 1);
    int *ptr2 = (int *)(*(aa + 1));
    printf( "%d,%d", *(ptr1 - 1), *(ptr2 - 1));
    return 0;
}
  int * ptr1 = ( int * ); &aa represents the address of the entire two-dimensional array, pointing to 1, +1 represents skipping the entire array, pointing to the address after the 10th, * (ptr1-1) points to 10 The positional dereference is 10;1 + aa & )(
int * ptr2 = ( int * + < /span>)); aa means that the address of the first element of the two-dimensional array is the address of the first row pointing to 1, +1 means skipping the first row and pointing to the second The address of the first element of the line is 6, * (ptr2-1) points to the position of 5, and the dereference is 5.1aa (* )(
So the answer to this question is 10,5

Guess you like

Origin blog.csdn.net/weixin_67131528/article/details/134041792