1. "Pointer" plus "Number"

Hi all,
let's say we have an array of char (thus array of "bytes"):
Code:
`char pool[1000];`
and let's say the 1st element of array is stored at address "00000000", the 2nd one at "00000001", the 3rd one at "00000002", ... , and the last one at "00000999":
Code:
```printf("%d\n", &pool[0]);   // == 00000000
printf("%d\n", &pool[1]);   // == 00000001
printf("%d\n", &pool[2]);   // == 00000002

.
.
.

printf("%d\n", &pool[999]);   // == 00000999```

And now the problem begins.
Let's say we have "a pointer to int" which we will give the address of the 1st element in array:
Code:
`int* pInt = &pool[0];   // It takes "4" first elements in array (sizeof(int) == 4)`
And now if we would like to "add some number to that pointer", let's say:
Code:
`pInt = pInt + 1;   // "pInt" now points to address "0000003" and no to "00000001"`
But "I want to" do that addition so that pointer will point to the address "00000001"!
Is there any way to do it?

Thanks.

2. You would have to say something like

Example:
Code:
`*pInt = *pInt + 1;`

3. I should have also mentioned you could also do this:

Example:
Code:
```*(pInt + 1) = 5;     // which is the same as pInt[1] = 5
*(pInt + *pInt) = 5; // which is the same as pInt[pInt[0]] = 5```

4. Hi master5001,
no I don't want to do anything with values, I would like to add to the pointer "pInt" a number for example 1, so that it would point to the address "1 byte" next to pInt and no "4 bytes" next to pInt.
Any ideas?

5. Sure.

Example:
Code:
`pInt = (char *)pInt + 1;`

6. Ohh, thaaaaaank you.
I can't believe I didn't try it before.

Now it works.

Thanks master5001

7. No problem bud.

8. Perhaps you could have done it like this as well.

Code:
```char* pInt = &poo;

pInt += 1;
printf("&#37;c", *pInt)

or

printf("%c", *(pInt + 1) );```
So that the internal scalar value would be set up to 1 byte, which means when you increment the pointer by 1. It moves one byte forward. Consequently if you declare it as int, it moves 4 bytes from the base memory location. And it is also advised to use the following methods to inclement the pointer.

Code:
`*(ptr + 1);`
The former would lead you to miss the base memory location, unless you have taken a backup of it.

ssharish

9. This is all well and good, but it means that pInt would actually be a pointer to a char. Which is not the same as being a pointer to an int.

10. I removes the explicit type casting.

ssharish

11. Which is good for moving the pointer, but he wanted the data to be treated as having integer width.

Let me word this differently. A char * moves in one byte increments but can only access single byte spans of data. An int * moves in sizeof int increments (so 2, 4 or 8 usually) and accesses a span of sizeof int bytes (so 2, 4 or 8 depending on the machine).

The OP wants a pointer that moves like a char * but reads spans as large as an int *. So that is why your code is misleading, ssharish2005. It doess not read spans sizeof int in width.

12. Originally Posted by Petike
Hi all,
But "I want to" do that addition so that pointer will point to the address "00000001"!
Is there any way to do it?
Yes, but it is a very bad idea.
On some systems you can read from an unaligned address as you want. However being unaligned it takes a least twice as long as internally it does two aligned fetches, and then combines them to give you the desired result.
On other system's you'll get some kind of crash such as a bus error.

So my advice is to forget about trying to do that, and just make sure everything is aligned according to the size of the data type instead.