Originally Posted by
Shin Natsume
Well yes ofcourse that makes sense. I was beginning to think I had read something wrong in the past. You a Computer Science major too?
I'm a physics major, but I've got 7-8 years of experience with C programming, and have amongst other things written a C compiler.
Originally Posted by
Shin Natsume
So would this be a good example?
Code:
void *ptr;
int num=1;
ptr=(void*)num;
cout<<*(ptr)<<endl;
That is not valid. You can't dereference a void pointer. If you change "void *" to "int *", it will be syntactically correct, but will likely crash when you run it, since "1" is most likely not a valid memory address.
Here's an example that will work:
Code:
int digits[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int *p = &digits[0];
unsigned int q = (int) p;
q = q + sizeof(int);
p = (int *) q;
cout << *p << endl;
This will advance the p pointer by sizeof(int) (=4 bytes), making it refer to the second element (that is, "1"). Technically, it's better to use uintptr_t (defined in stdint.h) instead of "unsigned int", but "unsigned int" will work on most 32 bit systems.
Originally Posted by
Capernicus
Dude, srs, threadjacking.
Sorry about that. I'll stop now, if Shin Natsume wants more information, he can PM me or something.
Bookmarks