void

Is 'void' a valid return value for a function?

戏子无情 提交于 2019-12-05 22:47:16
问题 private void SaveMoney(string id...) { ... } public void DoSthWithMoney(string action,string id...) { if(action=="save") return SaveMoney(string id); ... } Why won't C# let me return the void of the private function back through the public function? It even is the same data type "void"... Or isn't void a data type? Is the following code really the shortest workaround? if(action=="save") { SaveMoney(string id); return; } 回答1: void is not an actual return (data)type! void says there is no

casting 0 to void

北慕城南 提交于 2019-12-05 19:12:40
问题 On my implementation of C++ (Visual Studio 2008 implementation) I see the following line in <cassert> #ifdef NDEBUG #define assert(_Expression) ((void)0) I do not understand the need to cast 0 to void. It seems to me that #ifdef NDEBUG #define assert(_Expression) (0) or even simply #ifdef NDEBUG #define assert(_Expression) 0 would do, considering the contexts in which assert(expr) can be used. So, what's the danger of 0 of type int instead of 0 of type void in this case? Any realistic

drawbacks of a void pointer in C++ [closed]

℡╲_俬逩灬. 提交于 2019-12-05 18:28:02
A far as I know, a void pointer in C++ void* can point to anything. This might be quite useful (for me) if I want to develop a solution without using some sort of inheritance. But the question I want to know is whether there is any performance drawbacks in this approach? The biggest drawback is that making use of void pointers stops the compiler from being able to enforce type checking. Especially in a language that supports object oriented principles, I think it would be strange to be making heavy use of void pointers. You are more likely to find void pointers in C to mimic some polymorphic

Using void in functions without parameter?

 ̄綄美尐妖づ 提交于 2019-12-05 13:09:02
问题 In C++ using void in a function with no parameter, for example: class WinMessage { public: BOOL Translate(void); }; is redundant, you might as well just write Translate(); . I, myself generally include it since it's a bit helpful when code-completion supporting IDEs display a void , since it ensures me that the function takes definitely no parameter. My question is, Is adding void to parameter-less functions a good practice? Should it be encouraged in modern code? 回答1: In C++ void f(void); is

How can I resolve this case of “Useless use of a variable in a void context”?

我的未来我决定 提交于 2019-12-05 10:25:29
问题 How can I resolve this case of "Useless use of a variable in a void context"? For example: my $err = $soap_response->code, " ", $soap_response->string, "\n"; return $err; I get warnings like "Useless use of a variable in a void context"? Why? How can I resolve it? 回答1: In case you want to concatenate the arguments, use the "." operator or join : my $err = $soap_response->code. " ". $soap_response->string. "\n"; my $err = join '', $soap_response->code, " ", $soap_response->string, "\n"; Next

c incompatible types in assignment, problem with pointers?

淺唱寂寞╮ 提交于 2019-12-05 09:29:59
Hi I'm working with C and I have a question about assigning pointers. struct foo { int _bar; char * _car[SOME_NUMBER]; // this is meant to be an array of char * so that it can hold pointers to names of cars } int foofunc (void * arg) { int bar; char * car[SOME_NUMBER]; struct foo * thing = (struct foo *) arg; bar = thing->_bar; // this works fine car = thing->_car; // this gives compiler errors of incompatible types in assignment } car and _car have same declaration so why am I getting an error about incompatible types? My guess is that it has something to do with them being pointers (because

G++ error: ‘<anonymous>’ has incomplete type

余生长醉 提交于 2019-12-05 08:05:20
I am forced to use a third party dongle access library that provides an include file 'sense4.h' with code as follows: #if !defined _WINDOWS_ #define WINAPI #define CONST const typedef unsigned char UCHAR; typedef unsigned short USHORT; typedef unsigned int UINT; typedef unsigned long ULONG; typedef char CHAR; typedef char TCHAR; typedef void VOID; ... #endif /* !defined _WINDOWS */ ... unsigned long WINAPI S4Startup( VOID ); unsigned long WINAPI S4Cleanup( VOID ); ... The problem is that g++ 4.6.1 complains about lines of code where typedefed VOID is used: sense4.h:375:9: error: ‘<anonymous>’

Two function declarations with void and empty argument list

青春壹個敷衍的年華 提交于 2019-12-05 06:58:29
I'd like to know why the following code: void foo(void); void foo() { } is valid in gcc. In C, there is no such thing as overloading and above declarations (in fact, one of them is a definition) declare two different functions (the first one doesn't take any arguments, the second one could take any number of arguments of any types). However, if we provide a definition for the first function: void foo(void) { } void foo() { } a compilation fails this time due to redefinition. But still , the first code is correct and might be confusing as in the following: void foo(void); int main(void) { foo()

void* is literally float, how to cast?

不想你离开。 提交于 2019-12-05 04:44:13
So I'm using this C library in my C++ app, and one of the functions returns a void*. Now I'm not the sharpest with pure C, but have heard that a void* can be cast to pretty much any other *-type. I also know that I expect a float at the end somewhere from this function. So I cast the void* to a float* and dereference the float*, crash. darn!. I debug the code and in gdb let it evaluate (float)voidPtr and low and behold the value is what I expect and need! But wait, it's impossible to the same during compile time. If I write float number = (float)voidPtr; it doesn't compile, which is

Is void** an acceptable type in ANSI-C?

╄→尐↘猪︶ㄣ 提交于 2019-12-05 04:33:38
I have seen a function whose prototype is: int myfunc(void** ppt) This function is called in a C file as a = myfunc(mystruct **var1); where mystruct is typedef for one of structure we have. This works without any compilation errors in MSVC6.0, But when I compile it with some other C compiler, it gives an error at the place where this function is called with error message: Argument of type mystruct ** is incompatible with parameter of type void ** The argument of myfunc() is kept as void** because it seems to be a generic malloc kind of function to be called with various structure variable