What is the meaning of 'static void *' in C? [duplicate] - c

This question already has answers here:
What does "static" mean in C?
(20 answers)
Closed 6 years ago.
I'm new to C and recently when i'm learning zeromq (work related) I'm kinda confused with the static void *:
...
static void *
worker_task(void *args)
{
...
What is the exact the meaning of that line? I tried looking for answer, I thought that it was a pointer but It is kinda odd, given that pointer usually have a variable name after the '*'.
Thank you very much, I hope It's not rude of me for asking this seemingly "novice" question. :)

The function worker_task returns a void *.
The static keyword in front of the function definition means that the function is only viewable inside of the current compilation unit, i.e. a given object file, typically built from one source file with several include files. The function is not visible from other object files that may be linked with with one containing this function.

Related

FreeRTOS static array declaration with a macro identifier [duplicate]

This question already has answers here:
What does this mean by variable define in C [duplicate]
(2 answers)
Closed 3 years ago.
I am learning FreeRTOS from scratch. In order to do that,first, i start to investigate Task.c file.
In that file there are macros, functions and declarations.
But I am confused about the declaration meaning and i cant figure it out why?
In task.c the PRIVILEGED_DATA is used like this
PRIVILEGED_DATA static List_t pxReadyTasksLists[ configMAX_PRIORITIES ];/*< Prioritised ready tasks.*/
In the above ,static List_t type array declaration is made that is okey, but what is the meaning of the macro identifier in the beginning of the declaration and how it is possible a macro identifier is used in a declaration as a specifier?
Thanks.
In Mpu_Wrappers.h
#define PRIVILEGED_DATA
In FreeRTOSConfig.h
#define configMAX_PRIORITIES 5
If this was just simple C code the macro would just be kind of documentation. When the code passes from the preposessor the macro is expanded to nothing.
However in freertos this macro is there to help the MPU. You can read more here: https://www.freertos.org/FreeRTOS-MPU-memory-protection-unit.html

What does this statement mean in C: "(void)ptr;" [duplicate]

This question already has answers here:
What does (void)var actually do?
(4 answers)
Closed 5 years ago.
It may not be clear from the title. I came across the following code in an embedded STM32 project. I don't understand the line inside the function.
static void txend1(UARTDriver *uartp) {
(void)uartp; // what does this do? Is it a statement?
}
I've tried searching elsewhere online, but most results are casting pointers to void pointers, which I don't think this is. Thanks for the help!
this is just a portable way to suppress the warning on this unused uart parameter.
It has no effect, but compilers see that as used, and don't issue any warning.
Very useful when the prototype of the function is imposed / cannot be changed (callback function) but your implementation doesn't need this parameter.
(note that gcc favors the __attribute__((unused)) construct, easier to understand, but not compatible with all compilers)

C Code declares expected variable type after function call [duplicate]

This question already has answers here:
Alternative (K&R) C syntax for function declaration versus prototypes
(5 answers)
Closed 6 years ago.
Currently I am looking at some C code. The code was created to make a .dll for use in visual basic. As I was going through the usual rigor of linking a new c project in visual studio I came across something I haven't seen before. Here is an example:
BOOL WINAPI DLLMain(VEGETABLE Carrot, SOILTYPE Dirt, TOOLTYPE Spade)
{
return TRUE;
}
int WINAPI initialize(myVar)
char* myVar;
{
myCalc = do_a_thing(myVar)
return myCalc
}
The declaration of myVar as a char* is really the item in question here. My feeble google attempts haven't shown me anything like what I am seeing here. Is this simply an alternative way to perform type declarations, or is there something more interesting going on here?
Thanks!
This is the old way, called a K&R style function definition.
It's very old, like pre-C89. You don't see or use it in newly written code.

What does a line after function declaration but outside function itself [duplicate]

This question already has answers here:
Why would you declare variables in the middle of defining a function?
(2 answers)
Closed 7 years ago.
What does it mean if something like the two lines after the main function declaration appears in C code?
int main(argc,argv)
int argc;
char *argv[];
{
// main function body
}
I've never seen anything like this before. The code works just fine, but I'm curious to know what this means. Thanks!
This is the original (read: ancient) style of declaring argument parameters in K&R C. In ANSI C standards, the form you're likely familiar with has been the standard.
See also: What are the major differences between ANSI C and K&R C?
It is just one other way of declaring the datatype of arguments of that particular function.
That's "K&R" C. It's way out of date.
Don't use it, even if your compiler supports it. Arguments passed to a function defined in that manner will have undergone argument promotion so that every argument has the same size (via the same mechanism that varargs are promoted in up-to-date C code).
Such code also does not support function declarations/prototypes. And never try to "improve" such code by creating function prototypes - you'll break the argument promotion that the function is expecting.

Relevance of specifying an empty parameter list in C with void [duplicate]

This question already has answers here:
Is it better to use C void arguments "void foo(void)" or not "void foo()"? [duplicate]
(6 answers)
in c: func(void) vs. func() [duplicate]
(2 answers)
Closed 8 years ago.
Having done most of my dev deeds in C++ and C#, I find the habit in C of specifying an empty parameter list with "void" a bit peculiar, but seeing it in "trustworthy sources" makes me assume it's required-ish. The question is why?
To clarify, in for instance Visual Studio, I cannot with any combination of enabled C compiler switches, and disabled C++ compiler switches, get even as much as a compiler warning when writing this:
void foo_bar();
Then, what is it that makes it necessary-ish to instead declare it with this:
void foo_bar(void);
Is it just old C standardese that is lingering that is not needed with later C standards?
Backstory:
When declaring functions in C (any symbol actually) you need declare everything in the proper order (declare a function before using it) otherwise you'll get errors regarding undefined symbols.
Forward declaration of functions allows you to accomplish this by declaring only the function signature (without the body). This allows the compiler to identify function names (symbols) and later use them before they are completely defined.
When using forward declarations to export the publicly accessible functions of a library (in your public header files) you will declare the entire signature so that users of your library will know what parameters are expected.
When using forward declarations for your own internal use you (sometimes) won't need to declare the entire signature of the function, only the return type and name will suffice -- no parameters. Only when you finally define the function will you specify the parameter list.
Direct answer:
Yes it's just old C standardese and nothing more. Because the same C standardese says that empty brackets means an unknown number of parameters (not necessarily 0).
Related reading:
This question I've asked some time ago tries to fathom (like you) the utility of declaring functions with "an unknown number of parameters" -- apparently in a very misconstrued way.

Resources