473,657 Members | 2,496 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

integer to pinter conversion

Consider a function:

void *test_func(void )
{
return ((void *)-1);
}

While returning, the integer -1 is converted to void *.
Is this portable ?

Nov 15 '05 #1
22 2325
ju**********@ya hoo.co.in wrote:
Consider a function:

void *test_func(void )
{
return ((void *)-1);
}

While returning, the integer -1 is converted to void *.
Is this portable ?


No. While an integer may be converted to a pointer, the result
of this conversion is implementation-defined, with the obvious
exception of an integer constant expression with the value 0.

FWIW, you don't even need the cast in the code above.

Best regards.
--
Irrwahn Grausewitz (ir*******@free net.de)
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc frequent answers: http://benpfaff.org/writings/clc
Nov 15 '05 #2
ju**********@ya hoo.co.in wrote:

Consider a function:

void *test_func(void )
{
return ((void *)-1);
}

While returning, the integer -1 is converted to void *.
Is this portable ?


No.

--
pete
Nov 15 '05 #3

Irrwahn Grausewitz wrote:
ju**********@ya hoo.co.in wrote:
Consider a function:

void *test_func(void )
{
return ((void *)-1);
}

While returning, the integer -1 is converted to void *.
Is this portable ?


No. While an integer may be converted to a pointer, the result
of this conversion is implementation-defined, with the obvious
exception of an integer constant expression with the value 0.

FWIW, you don't even need the cast in the code above.


Is the conversion of integer to pointer and back again (pointer to
integer) will give the original integer ?

Nov 15 '05 #4
ju**********@ya hoo.co.in wrote:

Irrwahn Grausewitz wrote:
ju**********@ya hoo.co.in wrote:
>Consider a function:
>
>void *test_func(void )
>{
> return ((void *)-1);
>}
>
>While returning, the integer -1 is converted to void *.
>Is this portable ?


No. While an integer may be converted to a pointer, the result
of this conversion is implementation-defined, with the obvious
exception of an integer constant expression with the value 0.

FWIW, you don't even need the cast in the code above.


Is the conversion of integer to pointer and back again (pointer to
integer) will give the original integer ?


I can't find any such guarantee in the standard, so I'd say: no.

IMO, if you feel the need to convert pointers to ints and vice versa,
your program logic is broken.

Best regards
--
Irrwahn Grausewitz (ir*******@free net.de)
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc frequent answers: http://benpfaff.org/writings/clc
Nov 15 '05 #5
On Wed, 14 Sep 2005 01:19:36 -0700, junky_fellow wrote:

Irrwahn Grausewitz wrote:
ju**********@ya hoo.co.in wrote:
>Consider a function:
>
>void *test_func(void )
>{
> return ((void *)-1);
>}
>
>While returning, the integer -1 is converted to void *.
>Is this portable ?
No. While an integer may be converted to a pointer, the result
of this conversion is implementation-defined, with the obvious
exception of an integer constant expression with the value 0.

FWIW, you don't even need the cast in the code above.


Yes, you do, the only integer calues that can be converted to pointer
types without a cast are null pointer constants, and -1 isn't one of those.
Is the conversion of integer to pointer and back again (pointer to
integer) will give the original integer ?


No, converting an integer to a pointer can result in a trap
representation. In which case just looking at the value will cause
undefined behaviour.

Lawrence
Nov 15 '05 #6
Lawrence Kirby <lk****@netacti ve.co.uk> wrote:
Irrwahn Grausewitz wrote:
ju**********@ya hoo.co.in wrote:
>Consider a function:
>
>void *test_func(void )
>{
> return ((void *)-1);
>}
>
>While returning, the integer -1 is converted to void *.
>Is this portable ?

No. While an integer may be converted to a pointer, the result
of this conversion is implementation-defined, with the obvious
exception of an integer constant expression with the value 0.

FWIW, you don't even need the cast in the code above.


Yes, you do, the only integer calues that can be converted to pointer
types without a cast are null pointer constants, and -1 isn't one of those.

<snip>

Oops, you're right, I forgot about assignment constraints.

Best regards
--
Irrwahn Grausewitz (ir*******@free net.de)
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc frequent answers: http://benpfaff.org/writings/clc
Nov 15 '05 #7
In article <11************ *********@g49g2 000cwa.googlegr oups.com>,
<ju**********@y ahoo.co.in> wrote:
Is the conversion of integer to pointer and back again (pointer to
integer) will give the original integer ?


KR2 promises that it will [provided the sizes are compatable], but
C89 does NOT have that guarantee.

In the simplest case: conversion of the integer constant 0 to
a pointer results in the "null pointer constant", which is NOT certain to
be all-zeroes internally. Converting the null pointer constant
to an integer is not defined as returning 0.

Furthermore, C89 allows for the possibility that a null pointer
constant, when converted to pointer type other than void *, might take
on a different value, not necessarily the same for each type. Converting
that back to void * is not certain to result in a bit pattern which
is the same as the canonical null pointer constant: it is only
certain that no matter what sequence of pointer conversions one
undertakes, that all null pointers will compare equal.

--
Entropy is the logarithm of probability -- Boltzmann
Nov 15 '05 #8
ju**********@ya hoo.co.in wrote:
# Consider a function:
#
# void *test_func(void )
# {
# return ((void *)-1);
# }
#
# While returning, the integer -1 is converted to void *.
# Is this portable ?

The code is portable. What it means depends on the system.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I think that's kinda of personal; I don't think I should answer that.
Nov 15 '05 #9
SM Ryan <wy*****@tang o-sierra-oscar-foxtrot-tango.fake.org> wrote:
ju**********@y ahoo.co.in wrote:
#
# void *test_func(void )
# {
# return ((void *)-1);
# }
#
# While returning, the integer -1 is converted to void *.
# Is this portable ?

The code is portable.
Nope, it's not portable. Well, unless you refer to portable as in
beer, not in programming: you can print it out and carry it around.
By that definition, all code is portable.
What it means depends on the system.


Three very popular meanings:
1. You shoot yourself in the foot.
2. You shoot yourself in the foot, but don't notice.
3. You shoot yourself in someone else's foot.

Best regards
--
Irrwahn Grausewitz (ir*******@free net.de)
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc frequent answers: http://benpfaff.org/writings/clc
Nov 15 '05 #10

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

Similar topics

4
10459
by: Ray | last post by:
When a single-bit bitfield that was formed from an enum is promoted/cast into an integer, does ANSI C say anything about whether that integer should be signed or unsigned? SGI IRIX cc thinks it is an unsigned integer, so I see a +1 if the bit is set. Microsoft VC++ thinks it's signed, so I see -1 if the bit is set. Ex. typedef enum {
14
9314
by: junky_fellow | last post by:
Can anybody please explain this: When a value with integer type is converted to another integer type other than _Bool, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.
3
266
by: junky_fellow | last post by:
Consider a function: void *test_func(void) { return ((void *)-1); } While returning, the integer -1 is converted to void *. Is this portable ?
21
4107
by: Frederick Gotham | last post by:
I set about trying to find a portable way to set the value of UCHAR_MAX. At first, I thought the following would work: #define UCHAR_MAX ~( (unsigned char)0 ) However, it didn't work for me. Could someone please explain to me what's going on? I would have thought that the following happens: (1) The literal, 0, whose type is int, gets converted to an unsigned char.
10
3175
by: Mike S | last post by:
Does anyone know the logic behind why in VB.NET the result of a floating-point division ('/') is -rounded- on being converted to an integer type, such as with statements like Dim x As Integer = 2/3 'after assignment, x is 1, whereas a sane person would say it should be 0 Does Microsoft have a reason for this design decision? I understand that this type of rounding can reduce the overall error in long computation chains by reducing the...
6
13815
by: sarathy | last post by:
Hi, What is integer promotion? How is it different from arithmetic conversion? Regards, Sarathy
1
3773
by: charles_gero | last post by:
Hi all, I had a question about the topics in the subject and posted to comp.std.c, but feel it may also be appropriate here. Please excuse this crosspost if it is in bad form. I have a question about whether or not I am interpreting a nuance of the standard correctly, and the implications of said nuance. The sections in the C99 standard (and possibly older standards) that I will reference are as follows (typed out hopefully to avoid...
7
12266
by: Spoon | last post by:
Hello everyone, In my code, I use uint16_t (exactly 16-bit-wide unsigned integer type) and I need to print their value in base 10. http://www.opengroup.org/onlinepubs/009695399/basedefs/inttypes.h.html As far as I understand, the recommended method is: #include <inttypes.h>
12
3283
by: lithiumcat | last post by:
Hi, I bothered you a while back about storing integer values in void*. Now in a completely unrelated context, I'm trying to store pointer values in an integer type. So the basic question is, is it possible to convert a pointer into an integer, and then later (but on the same execution environment, ie the program has not exited, thus it's the same architecture, same compiler, same binary representations and so on) retrieve from the
0
8425
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8326
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8845
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8743
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8522
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8622
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6177
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2745
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.