455,515 Members | 1,848 Online
Need help? Post your question and get tips & solutions from a community of 455,515 IT Pros & Developers. It's quick & easy.

 P: n/a I have a function f with the following prototype: int f(void *). From my main() (or whatever) I can do things like int x = 123456 ; int y ; y = f(&x) ; which is fine. My question is, how can I pass the 123456 integer to f without using the intermediate x variable? Naively, what I would like to do is something like y = f(&123456) ; which of course results in a syntax errors. May 25 '07 #1
13 Replies

 P: n/a "K. Jennings"

 P: n/a "K. Jennings" wrote: > I have a function f with the following prototype: int f(void *). From my main() (or whatever) I can do things like int x = 123456 ; int y ; y = f(&x) ; which is fine. My question is, how can I pass the 123456 integer to f without using the intermediate x variable? Naively, what I would like to do is something like y = f(&123456) ; which of course results in a syntax errors. Why not "int f(int);", which is more direct and safer. -- cbfalconer at maineline dot net -- Posted via a free Usenet account from http://www.teranews.com May 25 '07 #3

 P: n/a On Fri, 25 May 2007 12:13:06 -0400, CBFalconer wrote: "K. Jennings" wrote: >>I have a function f with the following prototype: int f(void *). Frommy main() (or whatever) I can do things like int x = 123456 ; int y ; y = f(&x) ;which is fine. My question is, how can I pass the 123456 integer to fwithout using the intermediate x variable? Naively, what I would liketo do is something like y = f(&123456) ;which of course results in a syntax errors. Why not "int f(int);", which is more direct and safer. Because in the case that I am interested in the data buffer fed to f can contain lots of different data types, that will be dealt with appropriately in the right contexts. May 25 '07 #4

 P: n/a K. Jennings wrote: I have a function f with the following prototype: int f(void *). From my main() (or whatever) I can do things like int x = 123456 ; int y ; y = f(&x) ; which is fine. My question is, how can I pass the 123456 integer to f without using the intermediate x variable? You can't. Naively, what I would like to do is something like y = f(&123456) ; which of course results in a syntax errors. Even if it worked, what's `f` going to do with it? It's handed a pointer-to-object, but it doesn't know what kind of object. It can pass it along to another function that wants a `void*`, but the same applies to /that/. It can store it in a `void*` variable, but if that's to be pointful, someone somewhere has to know what to do with it. It could `free` or `realloc` it, but since your examples don't pass mallocated store, that would be ... unwise. Are you /sure/ that prototype is the one you want? -- Untyped Hedgehog The shortcuts are all full of people using them. May 25 '07 #5

 P: n/a Chris Dollin said: K. Jennings wrote: >I have a function f with the following prototype: int f(void *).From my main() (or whatever) I can do things likeint x = 123456 ;int y ;y = f(&x) ;which is fine. My question is, how can I pass the 123456 integer to fwithout using the intermediate x variable? You can't. Yes, he can, by changing the prototype of f so that it expects int rather than int *. Then he can pass the integer value like this: y = f(123456); provided, of course, that INT_MAX >= 123456 on his system. -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk email: rjh at the above domain, - www. May 25 '07 #6

 P: n/a Chris Dollin

 P: n/a K. Jennings

 P: n/a "K. Jennings"

 P: n/a "K. Jennings" wrote: CBFalconer wrote: >"K. Jennings" wrote: >>>I have a function f with the following prototype: int f(void *).From my main() (or whatever) I can do things like int x = 123456 ; int y ; y = f(&x) ;which is fine. My question is, how can I pass the 123456 integerto f without using the intermediate x variable? Naively, what Iwould like to do is something like y = f(&123456) ;which of course results in a syntax errors. Why not "int f(int);", which is more direct and safer. Because in the case that I am interested in the data buffer fed to f can contain lots of different data types, that will be dealt with appropriately in the right contexts. Then you better redefine f so that it knows the type involved, or define a carrying struct which imparts that info. You will have to use a void*, since you can't convert pointer types arbitrarily. But a void* can carry any data pointers information, and can be auto-converted back to the original type. -- cbfalconer at maineline dot net -- Posted via a free Usenet account from http://www.teranews.com May 25 '07 #10

 P: n/a Chris Dollin I have a function f with the following prototype: int f(void *).From my main() (or whatever) I can do things likeint x = 123456 ;int y ;y = f(&x) ;which is fine. My question is, how can I pass the 123456 integer to fwithout using the intermediate x variable? You can't. I think (modulo arguments about the OP's real intent) you can. It is unclear exactly what would constitute solution (e.g. no compiler/platform constraints were given and what does "not using an intermediate variable" really mean, etc) but you *can* do what he/she *seems* to want to do in standard C: y = f((int []){123456}); Someone else already pointed this out, but it seems worth another go. -- Ben. May 26 '07 #11

 P: n/a "K. Jennings" wrote: > I have a function f with the following prototype: int f(void *). From my main() (or whatever) I can do things like int x = 123456 ; int y ; y = f(&x) ; which is fine. My question is, how can I pass the 123456 integer to f without using the intermediate x variable? Naively, what I would like to do is something like y = f(&123456) ; which of course results in a syntax errors. Imagine the consequence of the following: int f(int *param) { return (*param)++ ; } Imagine, further, if the compiler combined all such references to constants, as it can with string literals: y = f(&123456); z = f(&123456); As I recall, FORTRAN passed parameters by reference, and it was indeed possible to modify "constants" in such a manner. -- +-------------------------+--------------------+-----------------------+ | Kenneth J. Brody | www.hvcomputer.com | #include | | kenbrody/at\spamcop.net | www.fptech.com | May 29 '07 #12

 P: n/a >"K. Jennings" wrote: >>... Naively, what I would like to do is something like y = f(&123456) ;which of course results in a syntax error. Indeed. As others have noted, C does not have this, but C99 does have something similar that will work. (Back before there was a C Standard, VMS C allowed "&constant" in just this way.) In article <46***************@spamcop.net> Kenneth Brody Imagine the consequence of the following: int f(int *param) { return (*param)++ ; }Imagine, further, if the compiler combined all such referencesto constants, as it can with string literals: y = f(&123456); z = f(&123456); If the constants are placed in some kind of physically-read-only memory (write-protected RAM, or ROM, for instance), the attempt to modify the constant will be trapped or ignored. Otherwise, "bad things happen". :-) >As I recall, FORTRAN passed parameters by reference, and it wasindeed possible to modify "constants" in such a manner. The mechanism in Fortran was not specified, and if a compiler used value-result, it could ignore attempts to modify constants this way. Most did tend to use by-value, though, and one could often achieve "interesting" effects by changing constants. -- In-Real-Life: Chris Torek, Wind River Systems Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603 email: forget about it http://web.torek.net/torek/index.html Reading email is like searching for food in the garbage, thanks to spammers. May 29 '07 #13

 P: n/a On 29 May 2007 18:47:27 GMT, Chris Torek As I recall, FORTRAN passed parameters by reference, and it was indeed possible to modify "constants" in such a manner. The mechanism in Fortran was not specified, and if a compiler used Right. value-result, it could ignore attempts to modify constants this way. Value-result, in the days before INTENT, _would_ modify the caller's 'constant' just like by-reference, from one call to another call or use. It would be distinguishable from by-reference only by multiple references (aliasing) within one call plus its subcalls. And such problems during one call would at least be near(er) to the cause and easier to debug; the real bastard is an unintended change caused in one part of the program that screws up things much later and far away. By-value (in only) wouldn't, but it wouldn't provide the FORTRAN (pre INTENT) semantics of allowing changes to any argument. Most did tend to use by-value, though, and one could often achieve "interesting" effects by changing constants. Just for the record, obviously you meant by-reference there. - formerly david.thompson1 || achar(64) || worldnet.att.net Jul 1 '07 #14

### This discussion thread is closed

Replies have been disabled for this discussion.