Casting a pointer to integer

A

anjali

Consider the following code:

#include <stdio.h>

int main(void)
{
char buf[100];
unsigned i;
i=(unsigned)buf +100;
printf("%u\n",i);
return 0;
}


Can anybody please tell me that is there is any way to write in the
memory area reserved by the buf array only by using the integer "i" ?

The question have some importance for me as I have to use such a
technique for pushing the parameters in a thread's stack using the
setjmp function in the Linux OS.

Thanks
 
E

Eric Sosman

anjali said:
Consider the following code:

#include <stdio.h>

int main(void)
{
char buf[100];
unsigned i;
i=(unsigned)buf +100;
printf("%u\n",i);
return 0;
}


Can anybody please tell me that is there is any way to write in the
memory area reserved by the buf array only by using the integer "i" ?

There is no portable way to do this. The conversion
from `char*' (which is what `buf' becomes in most contexts,
including this one) to `unsigned' produces an implementation-
defined result which may not be of any use in addressing
the memory later on. For example, on a machine with 64-bit
pointers and a 32-bit `unsigned' the conversion necessarily
loses data.
The question have some importance for me as I have to use such a
technique for pushing the parameters in a thread's stack using the
setjmp function in the Linux OS.

What you are doing is outside the bounds of the C language.
You are attempting to "look behind the curtain," and what you
find there is an artifact of the C implementation you happen
to be using at the moment. Your question isn't about C, but
about the internals of one implementation of C -- so you need
to find a newsgroup or other source of information specifically
about the system you're interested in.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,427
Latest member
HildredDic

Latest Threads

Top