Reputation:
I'm having a little fiddle with pointer arithmetic and just pointer in general and I've pulled together this code.
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
int main(int argc,char **argv){
void *ptr=NULL;
char a='a';
int i0=0;
char b='b';
int i1=1;
char c='c';
int i2=2;
ptr=&a;
//What do I do here to get to i0?
printf("%d",(int*)ptr); //and to print it out?
while(1);
return 0;
}
My question is exactly what I put into the comments. How do I get ptr to point to i0 without doing 'ptr=&i0;' using pointer arithmetic? Also how do I then print it out correctly for characters and integers(one method for char and one for int).
Thanks in advance. ;)
Upvotes: 0
Views: 734
Reputation: 6877
You'll have to do something like this:
int main(void)
{
union u
{
char ch;
int i;
} arr[] = { { .ch = 'a' }, { .i = 0 }, { .ch = 'b' }, { .i = 1 } };
union u *ptr = &arr[0];
printf("%d\n", ptr[1].i);
}
Not exactly what you wrote but would work.
Upvotes: 0
Reputation: 7068
the only thing I can think of is:
ptr += ((char*)&i0 - &a)
but not sure if it's valid anyway and it doesn't make much difference from ptr = &i0
as others pointed out, in accordance to the standard you can only do pointer arithmetic within bounds of a single object, like a struct or array, so the code above even if it might work is not really correct
Upvotes: 0
Reputation: 287755
The only way to get a pointer to the location of i0
is & i0
.
While some compilers may align i0
so that * ((int*) (((char*) ptr) - 1))
can be used to access i0
, that's not guaranteed. Compilers often reorder local variables or may not even store them in memory at all.
Upvotes: 7
Reputation: 108968
You can only do pointer arithmetic within a whole object (like an array). This works:
#include <stdio.h>
int main(void) {
int arr[100];
int *ptr;
arr[42] = 42;
ptr = arr; /* ptr points to the 1st element */
printf("%d\n", *(ptr + 42)); /* contents of the 43rd element */
return 0;
}
Upvotes: 1
Reputation: 229058
You can't do this.
Pointer arithmetic works on pointer to array elements that is, you can do arithmetic on a pointer that points to an element within an array to adjust the pointer to another element within that same array.
You cannot do pointer arithmetic on a pointer to a variable and make it point to another unrelated variable.
Upvotes: 1
Reputation: 39254
Your code doesn't make sense. You have no guarantee of where a, i0, b, i1, c and i2 are defined in memory when they're created - so you can't use pointer arithmetic to move from the address of a (ptr=&a) to i0.
If you want ptr to equal i0's location you can do ptr = &i0. If i0 was an integer (which it is) it will be 4 bytes big, so you can use pointer arithmetic to move through that integer 1 btye at a time if your pointer is void/char.
Upvotes: 1
Reputation: 320381
You can't get ptr
to point to i0
using pointer arithmetic. Pointer arithmetic only works within the bounds of a single array object (non-array variables are treated as arrays of size 1). You can't use pointer arithmetic to make a pointer skip from one standalone object to another.
Upvotes: 1
Reputation: 206508
In this code sample, without doing
ptr=&i0;
You cannot get ptr
to point to i0
portably because,
i0
is just a local variable & it's address is not stored in any other variable.
Upvotes: 0