# Thread: Pointing to the next character in an array?

1. ## Pointing to the next character in an array?

Example:
Code:
```int myArray[] = {5, 6, 7, 8};
int *myPointer = &myArray[0]```
Why is the following true?
Code:
`*(myPointer+1) == 6`
I get the concept, but shouldn't this only hold true if the variable is a one-byte variable? Using my logic, I would use this instead:
Code:
`*(myPointer+sizeof(int)) == 6`
Is it just that C compilers automatically take the size of the variable being pointed to into account?

2. Print the address of the variable in question, and then print the address of the variable + 1, and you will have your answer.

Quzah.

3. Originally Posted by quzah
Print the address of the variable in question, and then print the address of the variable + 1, and you will have your answer.

Quzah.
What's the best way to do this? Pretty much any way I try it, I get what I expected -- the compiler automatically takes into account the size of the variable. However, now I'm curious. Here's my code:
Code:
```#include <stdio.h>
int main ()
{
int myArray[] = {5, 6, 7, 8};
int * myPointer = &myArray[0];
printf("myPointer:\t0x%08lX\n", (unsigned long)myPointer);
printf("myPointer+1:\t0x%08lX\n", (unsigned long)(myPointer+1));
return 0;
}```
If I remember correctly, the size of a pointer, if you will, is dependent on the architecture? A 32-bit architecture has a pointer (i.e. address) size of 4 bytes, while a 64-bit architecture will have a pointer size of 8 bytes?

In other words, the maximum possible address location on a 32-bit architecture would be 0xFF FF FF FF, and on a 64-bit architecture it would be 0xFF FF FF FF FF FF FF FF.

Is that correct?

Then, further, is it safe to always use unsigned long for the size of a pointer? In other words, is the size of the unsigned long variable type interpreted differently on different machines with different architectures? Is this size determined at compilation or at runtime?

Edit: I just discovered %p as a printf() specifier, so I'll look into that. But my previous curiosity still holds! Any advice on either part is appreciated.

4. The whole thing also depends on your libraries and compiler. For instance, using MinGW on my 64-bit Windows 7 machine still gives sizeof(long)==4 and sizeof(long*)==4.

5. Use %p and type cast to (void *) to print addresses. The address of the next member in an array of type will be current address + sizeof( type ).

Quzah.

6. Edit: nevermind. I couldn't get the %x version to work the same as the %p version, but now I have:

Code:
```#include <stdio.h>
int main ()
{
int myArray[] = {5, 6, 7, 8};
int * myPointer = &myArray[0];

printf("myPointer:\t0x%08lx\n", (unsigned long)myPointer);
printf("myPointer:\t%p\n", (void*)&myArray[0]);
return 0;
}```

7. Notice the difference between printing the value of a pointer (in this case converted to unsigned long) and printing the value of the address of a pointer. It is similiar to the difference between printing the value of an int variable and printing the address of the int variable.

8. Yes, I made a mistake, I thought the %p took care of that for me (I thought I was supposed to pass the pointer's address to it for some reason, which makes no sense in retrospect).
I edited my above post, thanks.

Code:
```#include <stdio.h>
int main ()
{
int myArray[] = {5, 6, 7, 8};
int * myPointer = &myArray[0];

printf("Print address of beginning of array of 4-byte integers:\n");

printf("&myArray[0]:\t0x%08lx\n", (unsigned long)myPointer);
printf("&myArray[0]:\t%p\n", (void*)&myArray[0]);
printf("&myArray[0]:\t%p\n", (void*)myPointer);

// print address of next element of the array
printf("&myArray[1]:\t0x%08lx\n", (unsigned long)(myPointer+1));
printf("&myArray[1]:\t%p\n", ((void*)&myArray[0])+sizeof(int));
printf("&myArray[1]:\t%p\n", ((void*)myPointer)+sizeof(int));
return 0;
}```

9. Originally Posted by tabstop
The whole thing also depends on your libraries and compiler. For instance, using MinGW on my 64-bit Windows 7 machine still gives sizeof(long)==4 and sizeof(long*)==4.
If you compile for an x64 target sizeof(long*) should return 8... x64 pointers are 64bits (or should be).