what does (void) followed by variable at the end of void function mean? - Page 1
Forum section: electronics / general

author: sangar
author_rank: Regular Contributor
timestamp: August 06, 2017, 02:42:02 pm
content: Hello all,In my project's sample code, I have the following functionCode:void DataConf(bool status){SendData();(void)status;}In the above,what does the (void)status mean....It is some kind of return?......Thanks,Sangar

author: Nusa
author_rank: Super Contributor
timestamp: August 06, 2017, 02:46:12 pm
content: If that's the entirety of the code, it makes no sense to me, and the compiler will probably optimize it out as a do-nothing statement. The function itself is declared as void, so there are no return arguments.

author: rstofer
author_rank: Super Contributor
timestamp: August 06, 2017, 02:48:33 pm
content: It just references the parameter in such a way that the compiler doesn't complain about the parameter being unreferenced but it emits no code.https://stackoverflow.com/questions/1486904/how-do-i-best-silence-a-warning-about-unused-variablesOmit the statement and compile. See what you get. Then compile with the statement in place.

author: Nusa
author_rank: Super Contributor
timestamp: August 06, 2017, 03:01:10 pm
content: That makes sense, although that warning is often turned off by default, so you may not see any difference.I would take a more self-documenting approach myself:Code:void DataConf(bool /*status*/){SendData();}There's also a preprocessor attribute to accomplish the same result:Code:void DataConf(__attribute__((unused)) bool status){SendData();}

author: rstofer
author_rank: Super Contributor
timestamp: August 06, 2017, 03:06:10 pm
content: Of the various approaches, I wonder which are compiler agnostic.I think there is some validity to having the warning. There might be a debug version of the function that actually uses the parameter or, more likely, the function code really should have referenced the parameter and didn't.

author: Ian.M
author_rank: Super Contributor
timestamp: August 06, 2017, 03:35:35 pm
content: Although your example does nothing except suppress a compiler warning, in an embedded environment its not unusual to have various device registers mapped to C variables (with the volatile modifier so the compiler knows not to optimise away any accesses to them). Some of the registers will have side-effects when accessed, e.g clear a flag when read.Its quite common to see a read of such a register cast to void. E.g. something like:Code:(void)SPIBUF;in a routine that sends data over a SPI link as SPI is a full duplex clocked protocol so for every byte sent, one is received, and in many cases, unless unwanted bytes are read and discarded, the hardware will detect a buffer overflow, raise an error flag and halt the SPI peripheral.You have to look carefully at the code and know what names are used for device registers to be able to tell the different uses for (void)variable; apart.

author: alm
author_rank: Super Contributor
timestamp: August 06, 2017, 03:39:12 pm
content: Pretty much the same came up in this recent thread:https://www.eevblog.com/forum/microcontrollers/void-cast-as-a-function/

author: glarsson
author_rank: Frequent Contributor
timestamp: August 06, 2017, 03:55:36 pm
content: Quote from: Nusa on August 06, 2017, 03:01:10 pmNo. The standard C preprocessor does not have that attribute. Certain implementations might, e.g. gcc does, but it is not portable.

author: John Coloccia
author_rank: Super Contributor
timestamp: August 06, 2017, 04:04:19 pm
content: From a practical perspective, it's saying "I don't use this but I fully intend for it to be here", similar to including an empty default case in a switch statement with a comment inside. It's creates a very clear semantic intention of, "when nothing matches any of my cases, I absolutely intend for it to do nothing at all." Ditto for doing something like this://call a function that returns an int(void)func();You're explicitly throwing away the return type.It's really not a bad idea, though it's not common, and hopefully it shouldn't have to be done too often or you have to ask yourself why you have all of this unused stuff in your code in the first place.

author: rstofer
author_rank: Super Contributor
timestamp: August 06, 2017, 05:50:32 pm
content: Quote from: John Coloccia on August 06, 2017, 04:04:19 pmFor compatibility with 'zombie' code. Remnants left in calling procedures for which there is no source. Maybe in code provided as a library by some vendor.

author: John Coloccia
author_rank: Super Contributor
timestamp: August 06, 2017, 05:59:34 pm
content: Quote from: rstofer on August 06, 2017, 05:50:32 pmOh, sure...I totally understand how a lot of it ends up in code and it just has to be there. Also, stubs and things like that. But I also tend to clean up a lot that really doesn't need to exist. I guess the best reason to be precise about stuff like this, and "fix" any existing problems, is that you can then turn all of the checks and warning on, and see if anything new pops ups.

author: alm
author_rank: Super Contributor
timestamp: August 06, 2017, 06:14:11 pm
content: Another reason is to implement a generic interface, e.g. for function pointers / jump tables:Code:int (*foo[])(int, int) = { &foo1, &foo2 };int foo1(int x, int y) {(void)y;return x + 1;}int foo2(int x, int y) {(void)x;return y + 1;}

author: rstofer
author_rank: Super Contributor
timestamp: August 06, 2017, 06:57:37 pm
content: Quote from: alm on August 06, 2017, 06:14:11 pmThat's probably the best example I have seen!Obviously, foo3 returns x+y, foo4 returns x*y, and so on.Very clever!

author: rstofer
author_rank: Super Contributor
timestamp: August 06, 2017, 08:15:21 pm
content: Quote from: evb149 on August 06, 2017, 07:45:45 pmI was thinking about a dispatch table for a calculator of some kind. Perhaps it's both numeric and logical, we could extend the function table to include AND OR NAND NOR, etc.

author: tiger99
author_rank: Newbie
timestamp: August 07, 2017, 09:11:16 pm
content: There seem to be a lot of nonsensical answers here! Try this link for a proper explanation.https://stackoverflow.com/questions/1043034/what-does-void-mean-in-c-c-and-cIt has nothing at all to do with dead code or anything along these lines, nor does it mean that the function is not doing anything. The function may well, within itself, change global memory (not best programming practice) or manipulate hardware registers, timers, I/O pins etc. A function to set an I/O pin high, for instance, would not need a return value, it would always presume to have done its job. But if it was setting a timer it might be appropriate to return the previous value, and then you would have a return type of int or even long. And, a function reading the status of a pin would need a return value, and so not be declared void, unless (again, bad programming practice) it was instead putting the value read in global memory.The extreme case of void sillyfunction(void) does not mean that the function is silly and does nothing. It may well, for instance, contain code to flash a LED 3 times, every time it is called, an operation that needs no input parameters and does not need to return anything. Or it may be the halt() function, which neither needs a parameter nor ever can return.In modern C programming, void is used quite extensively to show the compiler that there is really not an error and there really should not be a parameter or a return value. Proper use, ALWAYS, where a value is not required, ever, enables the compiler to reliably detect true error conditions, when a function that needs a parameter does not get one, or a return value that may not be there is used by higher level code. One of the basic, and very necessary, compile-time checks is ensuring that each function has parameters of the same type every time it is called, and the return value is used as the same type consistently every time. Sometimes return values are discarded, of course, but we should try to use them consistently every time for any particular function and thereby avoid many possibly nasty and elusive bugs.But what would you do to check for consistency of parameters and return values of functions like printf()? All I can say is that writing compilers and syntax checkers is never trivial, and sometimes unspeakably horrible....

author: andersm
author_rank: Super Contributor
timestamp: August 07, 2017, 09:53:01 pm
content: Quote from: rstofer on August 06, 2017, 03:06:10 pmThere's none that works on all compilers. Modern ones understand casting to void, or assigning to self (var = var), but in some compilers those idioms will instead trigger warnings about statements without effects, while the thing that works for them will trigger warnings in other ones, and so on.

author: Nusa
author_rank: Super Contributor
timestamp: August 08, 2017, 12:05:20 am
content: Commenting out or omitting the name in the parameter declaration is probably the most portable of the methods mentioned.

author: Bruce Abbott
author_rank: Frequent Contributor
timestamp: August 08, 2017, 08:29:30 pm
content: Quote from: Nusa on August 06, 2017, 02:46:12 pmBut of course thatisn'tthe entirety of the code. Without seeing the function and variable definitions we can only guess what it does.Here's another example. Can you guess what this code doeswithoutexamining the header file? (hint, it's also an ASCII drawing)Code:MAINmAInMa1NMAINma1NmA1NmAiNMa1NMA1N ma1N mA1N maIn MaIN malN mA1Nma1nmA1NmaiNMainMAIN main Ma1N MAIN Ma1n mA1N MA1N maINmAin mAlNMa1nMAlNmAIN MainmaInMaInmA1N Ma1n maln mAin MaInMainmaInMaINmAiNMa1NMa1NMa1nmalnmaINmAinMaInma1Nma1NMainma1nMaINmAiNMa1NMa1NMa1nmalnmaINmAinMaInma1Nma1NMainmAInMa1NmAInMa1NmAInMa1NmAInMa1NmAInMa1N mAInMa1N mAInMa1NmAInMa1N mAInMa1N mAInMa1NmAInMa1N mAIn Ma1NmAInMa1N mAIn Ma1NmAIn Ma1NmAInMa1NMa1nma1Nma1Nma1Nma1Nma1N ma1N ma1N ma1Nma1Nma1Nma1Nma1Nma1Nma1Nma1Nma1NMainMAinmA1N maiN ma1N Main mAIN MainMAInMAINmAInMa1NMAINmAinma1NMaiNMAINmainMaINmalNMainmainMaInMainmAINmA1NmaiNMalNMa1NMAiNmaInmAln mainma1NMA1nMa1N MAiNma1nmAlnmain ma1NMA1nmAinMAlnMa1NmA1nMAiN ma1nmAlnmainMAln mAinma1Nma1Nma1NMAln Ma1N mA1n MAiN maInmAlnmainMAlnMa1NMAiNma1nmAlnmain ma1N MA1n mAin MAlnMa1NmA1nMAiNma1nmAlnmainMAlnmAinma1Nma1Nma1Nma1Nma1Nma1NMalnmainmA1NMAiNma1nmAlnmainMAlnmAinma1NMA1nMAiNmaInmAlnmainMAlnMa1NMAiNma1nmAlnmain ma1N MA1nmAinMAlnMa1NmA1nMAiNma1nmAlnmainMAlnmAinma1Nma1Nma1Nma1NMainMAIn

author: Ian.M
author_rank: Super Contributor
timestamp: August 08, 2017, 09:10:50 pm
content: @Brian,Does: NoIs: Yes - it looks like part of anObfuscated C Contestentry.

author: Nusa
author_rank: Super Contributor
timestamp: August 27, 2017, 07:11:26 pm
content: Quote from: Bruce Abbott on August 08, 2017, 08:29:30 pmApologies, I've been offline for nearly 3 weeks.The ascii graphic is easy. One output is the XOR of the inputs, the other is the AND of the inputs.I'll presume the obfuscated C code takes two boolean inputs and produces two boolean outputs using that logic.No, I'm not going to try to recreate a set of #defines for all the combinations of  to get that result.

author: Ian.M
author_rank: Super Contributor
timestamp: August 27, 2017, 07:35:14 pm
content: Spoiler for Bruce's reply#19:https://www.cise.ufl.edu/~manuel/obfuscate/heathbar