0

Could somebody explain exactly what the following C code does please? Specially the first line and the iterated line?

int * p = &my_numbers[0];

int i;
int j = 0;

for (i = 0; i < 6; i++)
{
  j += *(p + (i));
}

Does the compiler (GCC) know that when I increment the iterator, that I mean by sizeof(int) / 4 bytes instead of one byte? Is this the same for every compiler? Originally I was using:

  j += *(p + (i * sizeof(int)));

but that gave the wrong answer, however:

  j += *(p + (i));

did give the correct answer.

What would I have to write if I wanted to increment by only a single byte? (not sure why I would want to do this, I guess maybe the strongly typed nature of the language would allow me to use char to tell the compiler maybe?).

  • 1
    See http://stackoverflow.com/questions/394767/pointer-arithmetic for a great explanation of this. – Michael Dorgan Mar 05 '13 at 18:32
  • In addition to what @MichaelDorgan said, generally speaking, for a pointer `p` to some type `T` the operation: `p + i` is equivalent to: `p[i]`. – Nik Bougalis Mar 05 '13 at 18:32

5 Answers5

1

Yes. In pointer arithmetic, the pointer will be adjusted by the size of the type pointed to.

If you wanted single-byte pointer arithmetic, you'd have to use char* and casts. But you'd likely run into alignment problems if you're really dealing with ints.

Fred Larson
  • 60,987
  • 18
  • 112
  • 174
1

Since you used

int * p

The compiler knows it needs to find the next address that is 4 bytes away. If

char * p

was used instead, only a distance of 1 byte will be added each time.

The initial code

j += *(p + (i * sizeof(int)));

moves the pointer way past the address you would like it to be pointing at.

stonefruit
  • 195
  • 1
  • 10
  • what would happen if I incremented `void *`, is that defined behaviour? –  Mar 05 '13 at 18:40
  • `void *` is a generic pointer. You must cast it if you want to do anything usable with it – cipher Mar 05 '13 at 18:49
  • @dje1990 C doesn't support + or - on a void*. However some compilers , such as gcc, provides it as an extension, in which case void* is incremented/decremented by 1 byte. – nos Mar 05 '13 at 18:50
  • read the 'void pointers' section in http://www.cplusplus.com/doc/tutorial/pointers/ . Although it is a tutorial for C++, it shares a common explanation. – stonefruit Mar 05 '13 at 19:01
0

Because p is an pointer to an int type, it's arithmetics are best suited for int type. If the pointer pointed to char then it would have incremented by 1 byte .

cipher
  • 2,414
  • 4
  • 30
  • 54
0

Because p is an pointer to an int type, it's arithmetics are best suited for int type. If the >pointer pointed to char or void then it would have incremented by 1 byte .

Void pointer can't be incremented directly. You have to cast it to char, byte etc to increment it.

CasperGhost
  • 117
  • 7
0

Pointer arithmetic takes the size of the pointed-to type into account; given a pointer T *p, the expression *(p + i) computes the address of the i'th element of type T following p and dereferences it.

John Bode
  • 119,563
  • 19
  • 122
  • 198