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
Related
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.
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:
How are arguments evaluated in a function call? [duplicate]
(2 answers)
Sequence points and partial order
(3 answers)
Closed 9 years ago.
in C this works as I expect
void foo(int a, int b ) { printf("%i,%i ",a,b ); }
int main()
{
int i=1;
foo(i++,i);
foo(i++,i);
foo(i++,i);
foo(i++,i);
return(0);
}
output: 1,2 2,3 3,4 4,5
Now, the following does not work as I would have guessed:
void foo(int a, int b ) { printf("%i,%i ",a,b ); }
int main()
{
int a[] = { 100,200,300,400,500,600,700 };
int i=0;
foo(a[i++],a[i]);
foo(a[i++],a[i]);
foo(a[i++],a[i]);
foo(a[i++],a[i]);
return(0);
}
It returns
100,100 200,200 300,300 400,400
Following the logic of the previous example, I would have expected
100,200 200,300 300,400 400,500
My first suspicion was that the increment was only called after the function call, however, as the first example shows, i is indeed incremented inside the function call, unless it is used as an index for the array.
I also tried the same using foo(*(a+(sizeof(int)i++)),(a+(sizeof(int)*i))); just to check, and it also acts like the second example.
The compiler is gcc version 4.6.3
My question is, why does it work when I'm using the variable i directly as the function parameter, but not when I use the variable i as an index for an array which is used as the function parameter?
Both of your code invokes undefined behavior. You may get either expected or unexpected result.
Compile your code with -Wall flag. Compiler should give the warning:
[Warning] operation on 'i' may be undefined [-Wsequence-point]
Read the C_FAQ - 3.8. It explains this issue pretty well.
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.
This question already has answers here:
What is this strange function definition syntax in C? [duplicate]
(6 answers)
Closed 8 years ago.
When reading some FreeBSD source code (See: radix.h lines 158-173), I found variable declarations that followed the "function heading" in the definition.
Is this valid in ISO C (C99)? when should this be done in production code instead of just declaring the variables within the "function heading?" Why is it being done here?
I refer to the function heading the string that looks like this: int someFunction(int i, int b) {
That looks like K&R (pre-ANSI) style. I don't think it's valid C99, but are they using C99? Joel
I think you are referring to the "old-fashioned" pre-ANSI way of declaring parameters in C. It looked something like this:
int foo(a, b)
int a,
int b
{
/* ... */
}
That may still be valid in C99, and will be accepted by compilers for backward-compatibility reasons, but it should be considered deprecated/archaic.
Er. Maybe I'm misunderstanding your question, but i and b in that snippet are parameters to the function. It's not some compact way of declaring variables within the function, like:
int someFunction() {
int i, b;
When you call someFunction, you pass it those arguments:
someFunction(1, 2); // `i` will be `1` and `b` `2` within `someFunction`