Passing array as an argument [duplicate] - c

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.

Related

What is the significance of asterisks postfixing a variable type in a method header? [duplicate]

This question already has answers here:
difference between int* i and int *i
(9 answers)
Why is the asterisk before the variable name, rather than after the type?
(12 answers)
Closed 3 years ago.
I have been trying to learn C as a programming language, and have been trying to solve sample problems on site like LeetCode in C programs. When I was reading over some of the skeleton code that was provided as a function header for a problem on LeetCode that I want to solve in C, the function header had asterisks post fixing some of the types, specifically like this:
int* twoSum(int* nums, int numSize, int target, int* returnSize) {
/* Code goes here */
}
After doing a fair bit of reading, I learned that prefixing a variable with an asterisk when declaring a variable reserves the variable as a pointer, but I have not been able to find anything about what it means when the type specifier itself is post fixed with an asterisk.
The spaces there don't matter.
int* nums is identical to int *nums. So are int * nums and int*nums.
All four of these declare nums as pointer to int.
It's a matter of style preference (though I wouldn't use that last one), with no effect on the generated code.

pass function as parameter with and withou ampersand c [duplicate]

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).

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

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?

How can I know which type(int, double, float etc..) is currently holding of void pointer? [duplicate]

This question already has answers here:
C: Extrapolating type from void pointer
(4 answers)
Closed 5 years ago.
How can I know which type(int, double, float etc..) is currently holding of void pointer?
Suppose
void *p;
int x=10;
p=&x;
printf("%s",type_of_void_pointer(p));
double d=1.5;
p=&d;
printf("%s",type_of_void_pointer(p));
The first printf should print "int" whereas second should print "double"
Is there any way to write type_of_void pointer function?
You can't know the type from the content in anyway. From the void* itself it is no way possible to know this. It is all address. Even if you look into the content it is impossible to know the type of it.
All you know here is the address and that's it. You can even cast it to different type and interpret it different way.

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

Resources