Difference between void and void * [duplicate]

1

What is the difference between void * and void as a return type of a function ?

Example 1:

void *func_nome(int param){
      ...
  }

Example 2:

void func_nome(int param){
      ...
  }
    
asked by anonymous 13.10.2016 / 16:41

1 answer

2

When declaring a function in C, C ++ (and other derived languages, such as Java and C #) it is always necessary to declare the return type of the function. void is a special type to indicate that the function returns no value. It literally means "nothing." This is what some other languages (like Pascal) would define as a procedure ( procedure ).

Any tipo* indicates a pointer to that type. For example: int* is a pointer to integer and char* is a pointer to character. But void* is not a pointer to "nothing". This is a special case. It indicates a pointer to any type, as if it were a wildcard. It is using when it is not known what type of data is being pointed. It is used, for example, as a return of the malloc function, because the memory it allocates can be used for any type of data. It is up to the programmer to then coerce the intended type.

    
13.10.2016 / 17:08