foo(void) vs foo(void *)

Related searches

Functionally and syntactically speaking, is there a difference between a function whose prototype is int foo(void) and int foo(void *)?

I know the difference between, for example, int bar(int) and int bar(int *) - one of them is looking for an int, and the other is looking for an int pointer. Does void behave the same way?

From this answer on Software Engineering, void is treated specially depending on how it's used. In C and C++, void is used to indicate an absence of a data type, whereas void * is used to indicate a pointer which points to some data/space in memory that does not have a type. void * cannot be dereferenced on its own, and must be cast to another type first. This cast need not be explicit in C, but must be explicit in C++. (This is why we don't cast the return value of malloc, which is void *.)


When used with a function as a parameter, void means a total absence of any parameters, and is the only parameter allowed. Attempting to use void like a variable type or include other arguments results in a compiler error:

int foo(void, int);     //trying to use "void" as a parameter
int bar(void baz);      //trying to use "void" as an argument's type
main.c:1:8: error: 'void' must be the first and only parameter if specified
int foo(void, int);
       ^
main.c:2:14: error: argument may not have 'void' type
int bar(void baz);
             ^

It is similarly impossible to declare a variable with type void:

int main(void) {
  void qux;         //trying to create a variable with type void
}
main.c:5:8: error: variable has incomplete type 'void'
  void qux;

void as a return value for a function indicates no data will be returned. Since it is impossible to declare a variable of type void, it is impossible to catch the return value of a void function, even with a void pointer.

void foo(int i) { return; }

int main(void) {
  void *j;
  j = foo(0);

  return 0;
}
main.c:5:5: error: assigning to 'void *' from
      incompatible type 'void'
  j = foo(0);
    ^ ~~~~~~

The typeless void * is a different case. A void pointer indicates a pointer to a location in memory, but does not indicate the type of data at that pointer. (This is the used to achieve polymorphism in C, such as with the qsort() function.) These pointers can be tricky to use, however, as it is very easy to accidentally cast them to the wrong type. The code below won't throw any compiler errors in C, but results in undefined behavior:

#include <stdio.h>

int main(void) {
  double foo = 47.2;    //create a double
  void *bar = &foo;     //create a void pointer to that double
  char *baz = bar;      //create a char pointer from the void pointer, which
                        //is supposed to hold a double

  fprintf(stdout, "%s\n", baz);
}

The following code, however, is perfectly legal; casting to and from a void pointer never changes the value it holds.

#include <stdio.h>

int main(void) {
  double foo = 47.2;
  void *bar = &foo;
  double *baz = bar;

  fprintf(stdout, "%f\n", *baz);
}

47.200000

As a function parameter, void * indicates that the type of the data at the pointer you are passing in is not known, and it is up to you, the programmer, to properly handle whatever is at that memory location. As a return value, void * indicates that the type of the data being returned is not known or is typeless, and must be handled by the program.

int quux(void *);   //a function that receives a pointer to data whose type is not known, and returns an int.
void *quuz(int);    //a function that receives an int, and returns a pointer to data whose type is not known.

tl;dr void in a function prototype means "no data" and indicates no return value or no parameters, void * in a function prototype means "the data at the pointer this function is given does not have a known type" and indicates a parameter or return value whose pointer must be cast to a different type before the data at the pointer can be used.

Is there any difference between foo(void) and foo()? (is �void� just a , First we've been taught to use (void) when declaring a function and use () when calling, using a function Apart from that,. void foo() means "a function foo taking � As several other answers have stated, they are semantically the same. I find that it makes things easier to read variable declarations (or parameter declarations) from right to left. const int &amp; x would be read as “x is a reference to an integer t

foo(void) - function with no parameters

foo(void *) - function with one void * parameter

What is void *? It is just the pointer to the data with no specified type. It Can be casted to any other pointer type

unsigned add(void *arr)
{
   unsigned *uarr = arr;
   return uarr[0] + uarr[1];
}

Is there a difference between foo(void) and foo() in C++ or C?, void vs empty argument list c++ unnamed parameters void function with no parameters. Consider these two function definitions: void foo() { } void foo(void) { } . The above code will give us an error because we have used ‘foo(void)’ and this means we can’t pass any argument to the function ‘foo’ as we were doing in

Functionally and syntactically speaking, is there a difference between a function whose prototype is int foo(void) and int foo(void *)?

There is a difference:

int foo(void) declares a function that accepts no arguments.

int foo(void *) declares a function that accepts single argument of type void*.

In C++, int foo(void) is equvalent to int foo().

Why is the C function pointer void* (*foo) (void*) optimal?, because it can take any amount of arguments and return any data through a pointer. the void pointer argument could be an array or a pointer to a struct, union ,� In particular, focus on the call the foo vs the call to bar.foo is preceded with xorl %eax, %eax (X ^ X == 0, and is the shortest encoding for an instruction that zeroes a register on the variable length encoded x86_64, which is why its used a lot such as in setting the return value).

f() vs f(void) in C vs C++, Prefer f(void) in C to potentially save a 2B instruction per function call when targeting int foo(); int bar(void); int baz() { foo(); bar(); return 0; } Stylistic warnings are in bikeshed territory (*cough* -Wparentheses *cough*). You can always cast a void* to a Foo* whether or not the object it points at is actually a Foo. The lack of type information for the associated data means that the library can’t provide even a basic level of type safety by guaranteeing that later accesses to stored data use the same type as was stored originally:

void foo(void) and void foo(), What is the difference between void foo(void); and void foo(); ? assumes that foo() means a function that takes an int argument, or something. Answer / nitin sharma. Compiler will convert variable b to unsigned int b and then do the addition. Which will be greater than 6 and you got 1.

According to the C Standard, subclause 6.7.6.3, paragraph 14 [ISO/IEC 9899:2011],An identifier list declares only the identifiers of the parameters of the function.

Comments
  • An answer to a related question: stackoverflow.com/a/1043209/434551.
  • Related (although not dupes as I can see): Is there a difference between foo(void) and foo() in C++ or C? - What is the difference between function() and function(void)? - What does an empty parameter list mean?
  • What may be more interesting is the difference between foo(void) and foo().
  • void * ... must be cast to another type first, but may be done so without an explicit cast. Not true in C++. In C++ the conversion form void* must be explicit. P.S. calling a cast explicit is redundant since cast is by definition an explicit conversion.
  • Updated to reflect C/C++ differences, thank you for letting me know!
  • Essential answer, so it is the best one. I would just hilight how this is an exception in the (type) vs. (type *) couples universe because void is not actually a type.