This question already has answers here:
Why cast an unused function parameter value to void?
(2 answers)
Closed 2 years ago.
I saw the piece of the code. But do not know what the purpose is.
void a()
{
static const char *string = "STRING";
...
(void)string; // <---- What the purpose of the line is?
...
}
(void) before a variable like that creates an empty expression and is used to silence warnings about a variable not being used by the program.
In this specific case it would be better to simply comment out the variable declaration.
Related
This question already has answers here:
What's the point of const pointers?
(17 answers)
Closed 3 years ago.
While reviewing code, I occasionally see function prototypes with constant pointers like:
void Foo(int * const bar);
Often a programmer using such a function does not understand what he actually sees and the function is later used like this:
const int bla = 0;
Foo((int* const) &bla);
which, as far as I understand, results in undefined behavior. In this case, the constant pointer caused confusion.
My question is: When is a constant pointer (not a pointer to a constant) in a function prototype required for the code to be correct?
The const keyword is just used to avoid mistakes generating compilation errors if the variable is touched and to optimize code making the assumption that the variable does not change. It is never required. It also is of no interest at all to the caller, since C passes all parameters by value.
Both the usages of const, either const int * or int * const, are just a contract between the programmer and the compiler. They are never required.
This question already has answers here:
How do function pointers in C work?
(12 answers)
Closed 3 years ago.
While browsing the sqlite documentation for C I've found this as a parameter to a function, what does it mean?
int (*callback)(void*,int,char**,char**);
This is the prototype of a function pointer to a callback function.
Because it is used as an argument in another function, it implies that that actual function needs to be declared and defined somewhere in your code before it is used in the function as an argument. I.e. something like this:
//declaration - possibly defined in a header file, or at top of .c file where it is used
int __cdecl handlerFunction(void*,int,char**,char**);
//definition
int __cdecl handlerFunction(void *db,int element,char **data1,char **data2)
{
//code to handle some event that invokes this callback
return 0
}
This question already has answers here:
Function pointers and address of a function
(5 answers)
Closed 4 years ago.
I want to pass a function as parameter, but I am confused if I should pass it with ampersand or not. The following snippet works in both ways. Why?
#include <stdio.h>
int print(int pas){
return pas;
}
int func_print(int (*k)(int)){
(*k)(555);
}
int main(){
printf("%d",func_print(print));
printf("%d",func_print(&print));
return 0;
}
Function names are special.
When used without calling the function, it automatically is translated to a pointer to the function. So &print, print, *print, **print, ***print, etc. all evaluate to the same expression of type int (*)(int).
This question already has answers here:
What is this strange function definition syntax in C? [duplicate]
(6 answers)
Closed 9 years ago.
I have this strange function definition in my homework code and I don't really know what it's supposed to mean.
char *
sh_single_quote (string)
char *string;
{...}
Especially the "char *string;" line, what with the semicolon at the end.
It is K&R style declaration of a function in C language.
In C, you usually write a function as:
size_t strlen(const char *str)
{
//code
}
In K&R style this will be written as:
size_t strlen(str) <--- here you write only the param name
const char *str; <--- here you write the type along with param name!
{
//code
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
casting unused return values to void
Need for prefixing a function with (void)
Casting function returns to void
Have seen in quite few places that while invoking the function why do we explicitly the return type of the function ? ex:
(void) myhostnames ( char * something);
What is use of this (void), and how it differs from not using the same ?
Kindly clarify.
Perhaps the function returns something and to prevent warnings from the compiler / errors from lint, the caller explicitly "throws" away the return.