What does "static" in brackets in function parameters mean? [duplicate] - c

This question already has answers here:
static keyword inside array [] brackets [duplicate]
(1 answer)
What is the purpose of static keyword in array parameter of function like "char s[static 10]"?
(1 answer)
Closed 5 years ago.
I saw a function whose prototype was this:
int foo(int arr[static], size_t len);
I have learnt that it's possible to put const and volatile there, which should be adjusted to pointers pointing to objects with corresponding cv-qualifiers. But this time it doesn't seem quite the same as
int foo(static int *arr, size_t len);
as static shouldn't appear here at all.
So what is it at last?

Related

Call a char pointer in the function [duplicate]

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.

Can someone explain what does this mean? [duplicate]

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
}

Passing array as an argument [duplicate]

This question already has answers here:
Difference between [square brackets] and *asterisk
(5 answers)
Closed 9 years ago.
I know these two are equivalent:
int some_function(char n[])
and
int some_function(char *n)
is there any reason to prefer one over the other??
On seeing
int some_function(char n[])
compiler interprets it as
int some_function(char *n)
Both are same. First one prefer over second sometimes to let the other programmers know that an array is passed (i.e, pointer to array element) to the function.

bizarre C statement [duplicate]

This question already has an answer here:
What is the purpose of static keyword in array parameter of function like "char s[static 10]"?
(1 answer)
Closed 9 years ago.
void test(int x[static 10]);
int main()
{
int a[]={1,2,3,4,5,6,7,8,9,10,11};
test(a);
return 0;
}
void test(int x[static 10])
{
printf("%d",x[9]);
}
I was looking for bizarre C statements. I found this one, But could not understand what is the use of static 10 in that statement. Is it same as int x[10]?
Another thing, you can use volatile also, in place of static e.g int x[volatile 10]
Anybody knows what is the use of this kinda declaration?
PS: Compiled using GCC 4.6.3,
It's a hint for the compiler telling that the x pointer parameter points to the first element of an array of at least 10 elements.
For example:
test(NULL); // undefined behavior

What does this weird C++ definition mean? [duplicate]

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
}

Resources