Basic types in C

Such a quick, odd question: I passed the exam, and the question was:

char c = 'A' char *p =&c char **p2 = &p void *v = &p2 

Enter the type (i.e. int, void *, etc.) of the following expression:

  • &v
  • p2 + 1
  • v[0]

I replied:

  • void **
  • char**
  • ILLEGAL

and lost full credit. I just wanted to get a second opinion from someone before trying to get the points back.

  • I was not sure about this, but I thought that since v was a void pointer to a memory address, v would be void** . If v were dereferenced, it would be char**** .

  • p2 is char** , so adding 1 to it will still make it char** .

  • v[0] does not exist.

If someone could help me with such a stupid problem, I would be very grateful. Thanks.

+4
source share
2 answers

Your answers are correct for the reasons you gave (separately for the dereferencing bits of v , although it seems that you already know that you cannot play void * ).

+5
source

p2+1 is pending segfault and v[0] == *(v+0) == *v , which is invalid and therefore invalid. It should be char ** if invented.

0
source

Source: https://habr.com/ru/post/1386973/


All Articles