This question already has answers here:
What is array to pointer decay?
(11 answers)
Why can't we pass arrays to function by value?
(9 answers)
Difference between passing array and array pointer into function in C
(3 answers)
Closed 3 years ago.
In C, if you pass an array as an argument to a function, what actually gets passed? And Why?
See the standard.
On entry to the function, the size expressions of each variably modified parameter are
evaluated and the value of each argument expression is converted to the type of the
corresponding parameter as if by assignment. (Array expressions and function
designators as arguments were converted to pointers before the call.)
Related
This question already has answers here:
Argument conversion: (normal) pointer to void pointer, cast needed?
(1 answer)
Concept of void pointer in C programming
(16 answers)
Closed 2 years ago.
The pthread_exit function should take a void pointer as input. I'm wondering how come it's possible to pass the address of an integer variable (e.g. pthread_exit(&ret1) here) without performing a cast conversion.
This question already has answers here:
How do you read C declarations?
(10 answers)
Closed 4 years ago.
I have this syntax:
double (*(f(double (*)(int))))(int);
Do I understand correctly syntax above that f is function that gets pointer to function that receive int and return a double?
No; it's actually even more complicated than that.
cdecl.org glosses this type definition as:
declare f as function (pointer to function (int) returning double) returning pointer to function (int) returning double
In other words, the function pointed to by f takes a function pointer as an argument, and returns a function pointer. Both of those function pointers must be to functions which take an int as an argument and return a double.
You could simplify this definition a bit using an intermediate typedef as follows:
typedef double (*int_to_double_function)(int);
int_to_double_function (*f)(int_to_double_function);
This question already has answers here:
In C, are arrays pointers or used as pointers?
(6 answers)
Closed 6 years ago.
Yes, it has been asked before, many times, but I wanna know which one is more advisable?
typedef struct {
int a;
int addr[8];
} usr_command;
usr_command* p_usr_command;
int foo(int* parameter)
{}
So which one is less prone to be problematic?
foo(p_usr_command->addr);
or
foo(&p_usr_command->addr[0]);
?
In this case ( passing an array name vs. passing the address of the first object in the array as function argument), both the snippets are equivalent.
Quoting C11, chapter §6.3.2.1, Lvalues, arrays, and function designators (emphasis mine)
Except when it is the operand of the sizeof operator, the _Alignof operator, or the
unary & operator, or is a string literal used to initialize an array, an expression that has
type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points
to the initial element of the array object and is not an lvalue. [....]
However, in general, they are not the same.
array is an array of type int [8]
&(array[0]) is a pointer, type int *
As already suggested by EOF in his comment, try passing both variants to the sizeof operator and check the result to get the hands-on output.
This question already has answers here:
difference between the dot operator and arrow operator by structure object variable for tree creation in c or c++ [duplicate]
(3 answers)
Closed 7 years ago.
I' just got started in learning struct in c language.
i Thought "->" and "." were equivalent but i get the following error when using "->" instead of ".":
invalid type argument of '->' (have 'struct item')
a->b is short for (*a).b.
There is no difference between a->b and (*a).b. There is a difference between (*a).b and a.b, of course - which is that the * dereferences a first (which must be a pointer or an array).
This question already has answers here:
Logic behind sizeof() for character constants and function names [duplicate]
(2 answers)
Closed 9 years ago.
Why is the result of:
sizeof(function_name)?
1? Somebody in community said that sizeof(main) is 1 but sizeof(main()) is 4. Here main is returning int, so it is returning 4. but if we do declaration as char main() instead of int main() it will return 1 only.
It's illegal to use sizeof on a function.
C99 §6.5.3.4 The sizeof operator
The sizeof operator shall not be applied to an expression that has function type or an
incomplete type, to the parenthesized name of such a type, or to an expression that
designates a bit-field member.
So if foo is the function name, sizeof(foo) is illegal, but sizeof(foo()) applies sizeof on the return value of foo(). main() returns int in standard C, it's 4 bytes on most machines today.
sizeof(char) equals 1, always and everywhere.
So everything declared char would do also, and this already at compile-time.