473,811 Members | 3,299 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

CAN WE TYPE CAST AN INTEGER AS (VOID *)X..(Like can I return a value (void *)x)

now suppose I have declared an integer value inside a function as
int x;
now if the return type of the function is of type (void *) then can I
write
return((void *)x) in side the function?
I came across this in a document on multithreading in C..I can post the
exact portion of code which works correctly with such an assignment if
you people want.
Please clarify my doubt.I hope I have conveyed it properly.

Jan 29 '06
12 2633
Ico wrote:
Vladimir S. Oka <no****@btopenw orld.com> wrote:
Ico wrote:
Hello AbhishV00ek,

Abhishek <ab************ *@gmail.com> wrote:

Vladimir, I think you misread the details in the OP's question: as far
as I onderstood, he is not returning the address of x, but the *value*
of x, casted to void * :
return((void *)x)


Apart from the cast, this is just as valid as returning x intself.

Ico


Vladimir, you muppet! You are henceforth barred from posting before 9 AM
and/or before having at least three (3) mugs of strong coffee!

The above applies to my previous reply, especially (which I still can't
see, and think will have to do un/re-subscribe to c.l.c dance again)!

Clerk, please strike Vladimir's statements in this case.

I'm kneeling on some corns for a while... :-(

Cheers

Vladimir

--
Hark ye, Clinker, you are a most notorious offender. You stand
convicted of sickness, hunger, wretchedness, and want.
-- Tobias Smollet

Jan 30 '06 #11
Eric Sosman wrote:
The O.P. has run across a function whose return type is
`void*' <ot> because of the requirements of a framework that
is not topical here </ot>. However, in the context of the
actual function it makes more sense to return an `int'. The
code tries to do so by converting the `int' to a `void*';
presumably, the caller retrieves the `void*' and converts it
back to an `int' again. The O.P. asks whether C guarantees
that this will work as desired.

The answer (may I have the envelope, please?) is "No."

That said, the dubious practice will in fact work as desired
on many implementations (I suspect it might fail on AS/400, but
I'm not sure of that). Completely portable approaches exist,
but some programmers are too lazy or too unimaginative to use
them. The O.P. is looking at such a programmer's product.


A portable solution within this framework is to make x static
and return its address. Then convert back to (int *) in the
calling function.

Jan 30 '06 #12


Old Wolf wrote On 01/30/06 08:28,:
Eric Sosman wrote:
The O.P. has run across a function whose return type is
`void*' <ot> because of the requirements of a framework that
is not topical here </ot>. However, in the context of the
actual function it makes more sense to return an `int'. The
code tries to do so by converting the `int' to a `void*';
presumably, the caller retrieves the `void*' and converts it
back to an `int' again. The O.P. asks whether C guarantees
that this will work as desired.

The answer (may I have the envelope, please?) is "No."

That said, the dubious practice will in fact work as desired
on many implementations (I suspect it might fail on AS/400, but
I'm not sure of that). Completely portable approaches exist,
but some programmers are too lazy or too unimaginative to use
them. The O.P. is looking at such a programmer's product.

A portable solution within this framework is to make x static
and return its address. Then convert back to (int *) in the
calling function.


Yes, <ot> but that's not a good idea in multi-
threaded code, which is what the O.P. is studying.
As it happens, the POSIX multi-threading framework
dictates that the signature of the function must be

void* function(void*)

.... and it's easy to use the argument as a pointer
to a place where the "returned" value -- int, double,
or whatever -- can be deposited. </ot>

Another safe way to disguise a smallish integer
as a pointer is to point at something:

char fake[10];
...
void* function(void* arg) {
int x;
...
return &fake[x]; /* 0 <= x <= 10 */
}
...
/* caller: */
int x = (char*)result_o f_f - fake;

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

Jan 30 '06 #13

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

Similar topics

10
53783
by: Jay | last post by:
How can I cast Integer value 450 to a char array "450 "? in a c program? thanks in advance for any help.
13
2610
by: yohji | last post by:
Hi,everyone! I am a beginner of C.When I studied C,I found a problem which isn't described in many C teaching books.It is like this: I found some of int or char* functions can be used as void function.For example: we can use int function printf() like this : printf("hello,world!\n"); How it is like a void function! Also we can use char* function ltoa()like this: ltoa(ltemp,buffer,10); Why??
15
29284
by: TC | last post by:
What does it mean for an integer to have a null value? I am trying to use the DataView.Find method. That method has an integer return type which contains the "index of the row in the DataView containing the sort key value specified; otherwise a null value if the sort key value does not exist." By "null value", does it mean System.DBNull? (I thought only objects could evaluate to System.DBNull.) How can I test whether an integer variable...
7
1931
by: Rudy | last post by:
Hello All! I have a value in a textbox(txbTableIDm.Text ) that I would like to use in a paremiter in a SP I wrote, and then have the select statement work off that parememter, retireive a diffrent value in another(txbCredits) texbox. Heres the code: Public Sub GetCredit() Dim ConCred As SqlConnection Dim strCred As String Dim cmdCred As SqlCommand
6
1897
by: Tim Roberts | last post by:
I've been doing COM a long time, but I've just come across a behavior with late binding that surprises me. VB and VBS are not my normal milieux, so I'm hoping someone can point me to a document that describes this. Here's the setup. We have a COM server, written in Python. For completeness, here is the script: ----- testserver.py ----- import pythoncom
3
1212
by: John Dann | last post by:
A question that I'm in two minds about - maybe someone more experienced could offer some advice? Let's say I have a method in a class whose function is to generate an image (But it could equally apply to any more complex object that the method might generate.) Then it seems to me that I have two options: 1. Set the image as the return value for the function.
25
4522
by: Christian Christmann | last post by:
Hi, the ANSI-C 99 standard specifies that the main function has "int" as return type. However, there are still lots of people declaring "void" as main return type. Did previous ANSI-C standards defined "void" as return type or is this issue just a lack of C knowledge? Regards,
7
5292
by: llothar | last post by:
When i use -W4 on visual c 7.0 i get warning C4054 translator1.c(1703) : warning C4054: 'type cast' : from function pointer 'void * (__cdecl *)(se_agent *)' to data pointer 'void *' translator1.c(1703) : warning C4152: nonstandard extension, function/ data pointer conversion in expression whenever i cast a function pointer to a void* or back. Is there any reason for this warning ? Looks like good C code for me.
16
2706
by: PeterAPIIT | last post by:
Hello all C++ expert programmer, i have wrote partial general allocator for my container. After reading standard C++ library and code guru article, i have several questions. 1. Why allocator write forward declaration then allocation for void* rather than directly wrote allocator first ?
1
1967
by: Reena NET | last post by:
I have tried ADO, now I've moved to SQLClient and I still cannot retrieve the return value from my procedure. Can someone please help me??! Thanks. Here is my VB code: cmd.CommandText = "usp_executeMerge" cmd.CommandType = CommandType.StoredProcedure Dim retparm As New SqlClient.SqlParameter retparm.ParameterName = "@retval"
0
9607
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
10395
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
10408
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
10137
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
7673
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
5561
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...
0
5700
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3874
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3026
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.