0
#include <stdlib.h>
#include <stdio.h>
long double* n = (long double*) malloc(256);
long double d = *(n + 3); 

Did we just increment n by 3 bytes? Or did we increment by (3 times the number of bytes in a long double)?

Alternatively, is C smart enough to know that n points to an array of long doubles, or is C stupid, where it needs to be told exactly how many bytes to advance the pointer?

Toothpick Anemone
  • 4,290
  • 2
  • 20
  • 42
  • 1
    Does this answer your question? [Character pointers and integer pointers (++)](https://stackoverflow.com/questions/2605689/character-pointers-and-integer-pointers) – 0x4d45 Jan 12 '20 at 19:26
  • Also, instead of `*(n + 3)` a C programmer would just write the shorter and nicer equivalent `n[3]` – Antti Haapala -- Слава Україні Jan 12 '20 at 19:28
  • In C pointer arithmetic works the same way as array indexing. The compiler knows that `n[1]` accesses the 10th... bytes (if `long double` is size 10). So `*(n + 1)` is the same. – Weather Vane Jan 12 '20 at 19:29
  • Pointer arithmetic is done in terms of objects, not bytes. If `p` points to a 4-byte `int` object, then `p+1` points to the next 4-byte `int` object. If `p` points to an 8-byte `double`, then `p+1` points to the next 8-byte `double`. – John Bode Jan 12 '20 at 19:36
  • `n` has type `long double *` , not sure where "smart enough to know" comes into this – M.M Jan 12 '20 at 22:12

0 Answers0