473,406 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Casting a pointer to integer

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

Nov 14 '05 #1
1 1356


anjali wrote:
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.

--
Er*********@sun.com

Nov 14 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ghostdog | last post by:
hi, i got this opengl/c++ code: <code> void render(CMesh *mesh){ ... float *pVertices; int *pIndices;
5
by: dis | last post by:
I've been going through my code and clearing away some of the compiler warnings that i'm generating and i've come across areas where i cast pointers to integer values. The Visual Studio compiler...
4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
1
by: rahul8143 | last post by:
hello, In kernel source code there is ip_fragment.c file my question is regarding pointer function and casting for that look at required snippet from that file There is structure defined for...
24
by: pinkfloydhomer | last post by:
Is it well-defined to make a cast from a pointer to an int and back? Like: typedef struct { int whatever; } S; int main(void) {
13
by: ballpointpenthief | last post by:
Have a look at this #include <stdio.h> #include <string.h> int track = 0; char this = "why "; char that = "not?"; int main(void) { track++;
7
by: William S Fulton | last post by:
I'm looking for the name of the following casting style in order to do some reading around on it and why it is sometimes used. unsigned long long ull = 0; void * ptr = 0; ull = *(unsigned...
11
by: jois.de.vivre | last post by:
I am interfacing with a third party API (written in C, if that matters) that has an "event handler" function with the following definition: void event_handler(int event_code, unsigned long...
32
by: alex.j.k2 | last post by:
Hello all, I have "PRECISION" defined in the preprocessor code and it could be int, float or double, but I do not know in the code what it is. Now if I want to assign zero to a "PRECISION"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.