[Solved] Exam Q&A that I don’t understand [closed]


  void main ( void )
  {
    int a[] = {22, 33,44};

‘a’ is a static array (or string) of 3 int, 22, 33, and 44.

    int *x = a;

‘x’ is an int pointer, initialized to point to the same static array as ‘a’.

    printf (" sizeof ( int )=% lu ", sizeof (int ));

Prints the number of bytes [4] required to represent an int type on this system.

    printf ("x=%p, x [0]=% d\n", x, x [0]);

Prints the memory address where the int pointer ‘x’ is currently pointing[0x7fff29af6530],
then also prints the integer value [22] stored in the [4] bytes starting at that address.
(Note: ‘x[0]’ is the same as ‘*x’).

    x = x + 2;

    x   [0x7fff29af6530]
   +2               + 8 (or (2 * 4) or (2 * sizeof(int)))
  ----  ----------------
new x   [0x7fff29af6538]

Advance the pointer ‘x’ 8 bytes.

The effect on ‘x’ is that it will now be pointing at its original memory address plus '(2 * sizeof(int))' bytes. ‘a[2]’ resolves to the same location.

    printf ("x=%p, x [0]=% d\n", x, x[0]);

Prints the memory address where the int pointer ‘x’ is currently pointing [0x7fff29af6538]
then also prints the integer value [44] stored in the [4] bytes starting at that address.

Hence; ‘x’ now resolves to the same address as ‘&a[2]’; and ‘*x’ resolves to the same number as ‘a[2]’.

   }

solved Exam Q&A that I don’t understand [closed]