"Pointer" plus "Number"
let's say we have an array of char (thus array of "bytes"):
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":
printf("%d\n", &pool); // == 00000000
printf("%d\n", &pool); // == 00000001
printf("%d\n", &pool); // == 00000002
printf("%d\n", &pool); // == 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:
And now if we would like to "add some number to that pointer", let's say:
int* pInt = &pool; // It takes "4" first elements in array (sizeof(int) == 4)
But "I want to" do that addition so that pointer will point to the address "00000001"!
pInt = pInt + 1; // "pInt" now points to address "0000003" and no to "00000001"
Is there any way to do it?
You would have to say something like
I should have also mentioned you could also do this:
*(pInt + 1) = 5; // which is the same as pInt = 5
*(pInt + *pInt) = 5; // which is the same as pInt[pInt] = 5
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.
pInt = (char *)pInt + 1;
Ohh, thaaaaaank you.
I can't believe I didn't try it before.
Now it works.
Perhaps you could have done it like this as well.
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.
char* pInt = &poo;
pInt += 1;
printf("%c", *(pInt + 1) );
The former would lead you to miss the base memory location, unless you have taken a backup of it.
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.
I removes the explicit type casting.
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.
Yes, but it is a very bad idea.
Originally Posted by Petike
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.