void

Why cast “extern puts” to a function pointer “(void(*)(char*))&puts”?

限于喜欢 提交于 2019-12-04 05:14:58
I'm looking at example abo3.c from Insecure Programming and I'm not grokking the casting in the example below. Could someone enlighten me? int main(int argv,char **argc) { extern system,puts; void (*fn)(char*)=(void(*)(char*))&system; char buf[256]; fn=(void(*)(char*))&puts; strcpy(buf,argc[1]); fn(argc[2]); exit(1); } So - what's with the casting for system and puts? They both return an int so why cast it to void? I'd really appreciate an explanation of the whole program to put it in perspective. [EDIT] Thank you both for your input! Jonathan Leffler , there is actually a reason for the code

Conflicting types and previous declaration of x was here…what?

风格不统一 提交于 2019-12-04 05:07:42
I've been teaching myself C for a few months when I have time, and I have run into a problem I am not sure how to fix. Specifically, when I try to compile this using gcc, I get: geometry.c:8: error: conflicting types for ‘trapezoid’ geometry.c:7: note: previous declaration of ‘trapezoid’ was here geometry.c:48: error: conflicting types for ‘trapezoid’ geometry.c:7: note: previous declaration of ‘trapezoid’ was here geometry.c:119: error: conflicting types for ‘trapezoid_area’ geometry.c:59: note: previous implicit declaration of ‘trapezoid_area’ was here geometry.c: In function ‘cone_volume’:

Return nothing or return void— What exactly does C# do at the end of a void Function() call?

微笑、不失礼 提交于 2019-12-04 04:21:07
问题 Consider the following C# function: void DoWork() { ... } The C# documentation states: When used as the return type for a method, void specifies that the method does not return a value . That seems straight-forward enough and, in most cases, that suits as a fair definition. However, for many lower-level languages (i.e. C) "void" has a slightly different meaning. Specifically, all blocks of code must return something . Therefore, void is a representation of the null pointer which is a

How to use .NET reflection to determine method return type (including void) and parameters?

好久不见. 提交于 2019-12-04 03:40:36
how to know the number and type of parameters? how to know the return type? how to check whether the return type is void? Jon Skeet Use MethodInfo.ReturnType to determine the return type, and MethodBase.GetParameters() to find out about the parameters. ( MethodInfo derives from MethodBase , so once you've got the MethodInfo via Type.GetMethod etc, you can use both ReturnType and GetParameters() .) If the method is void , the return type will be typeof(void) : if (method.ReturnType == typeof(void)) 来源: https://stackoverflow.com/questions/3456994/how-to-use-net-reflection-to-determine-method

Towards understanding void pointers

为君一笑 提交于 2019-12-04 03:25:07
In my answer I mention that dereferencing a void pointer is a bad idea. However, what happens when I do this? #include <stdlib.h> int main (void) { void* c = malloc(4); *c; &c[0]; } Compilation: gcc prog.c -Wall -Wextra prog.c: In function 'main': prog.c:4:2: warning: dereferencing 'void *' pointer *c; ^~ prog.c:5:4: warning: dereferencing 'void *' pointer &c[0]; ^ prog.c:5:2: warning: statement with no effect [-Wunused-value] &c[0]; ^ Here is an image from Wandbox for those who say it didn't happen: and a Live demo in Ideone. It will actually try to read what the memory that c points to has,

C++ send any type of argument to a function

╄→гoц情女王★ 提交于 2019-12-04 03:11:25
问题 Here it goes: I want to create a void function that will receive two well-known type of values and another one that could be anything. The code would be like this: void change_settings(string element, short setting, ??? value) { switch (setting) { case ST_NAME: // Cast value to string or char* and change element.name break; case ST_AMOUNT: // Cast value to integer and change element.amount break; case ST_ENABLED: // Cast value to boolean and change element.enabled break; } } I tryied to make

casting 0 to void

∥☆過路亽.° 提交于 2019-12-04 01:28:13
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 examples? Kerrek SB The only purpose of the complicated expression (void)0 is to avoid compiler warnings. If

Casting a void pointer to a struct

回眸只為那壹抹淺笑 提交于 2019-12-03 16:19:11
问题 I started feeling comfortable with C and then I ran into type casting. If I have the following defined in an *.h file struct data { int value; char *label; }; and this in another *.h file # define TYPE void* How do I cast the void pointer to the struct so that I can use a variable "TYPE val" that's passed into functions? For example, if I want to utilize the value that TYPE val points to, how do I cast it so that I can pass that value to another functions? 回答1: (struct data*)pointer will cast

Why using 0 as default non type template parameter for void* is not allowed

最后都变了- 提交于 2019-12-03 12:45:48
Why does the following code fail to compile? Even though it is legal to do void* ptr = 0; template <void* ptr = 0> void func(); int main() { func(); return 0; } I ask because I found that a very trusted source did something similar and it failed to compile on my machine NOTE Should have posted the compiler error along with my question so here it is so_test.cpp:1:23: error: null non-type template argument must be cast to template parameter type 'void *' template <void* ptr = 0> ^ static_cast<void *>( ) so_test.cpp:1:17: note: template parameter is declared here template <void* ptr = 0> ^ so

Call a void* as a function without declaring a function pointer

自作多情 提交于 2019-12-03 12:18:23
问题 I've searched but couldn't find any results (my terminology may be off) so forgive me if this has been asked before. I was wondering if there is an easy way to call a void* as a function in C without first declaring a function pointer and then assigning the function pointer the address; ie. assuming the function to be called is type void(void) void *ptr; ptr = <some address>; ((void*())ptr)(); /* call ptr as function here */ with the above code, I get error C2066: cast to function type is