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

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

Related

Variable passed as a parameter won't increment, why? [duplicate]

This question already has answers here:
What's the difference between passing by reference vs. passing by value?
(18 answers)
Closed 11 months ago.
Here I wrote this dummy code to show what my problem (in a more complex code) is. I am using implicit functions with a parameter. The function should assign a new value to the variable passed as a parameter, but I noticed that while this works passing the address of the variable (i.e. the parameter is a pointer, variable i_1 in my code), it won't work passing the variable itself (i_2 in this code). Why does this happen?
#include <stdio.h>
#include <stdlib.h>
void plusfivepointer(int * pa)
{
*pa+=5;
}
void plusfive(int a)
{
a+=5;
}
int main(void)
{
int i_1, i_2;
i_1=i_2=0;
printf("i_1=%d i_2=%d\n", i_1, i_2);
plusfivepointer(&i_1);
plusfive(i_2);
printf("i_1=%d i_2=%d\n", i_1, i_2); //i_2 won't increment
return EXIT_SUCCESS;
}
Because when you don't pass a variable by reference the function creates a local variable with the same name and works the process defined in the function on that local variable and that variable vanishes when the function pop out of the stack.
You need to pass by reference to alter the variable you passed in the function.

How to implement sizeof function similar to sizeof operator in c? [duplicate]

This question already has answers here:
How to find the size of a variable without using sizeof
(12 answers)
Closed 2 years ago.
I want to implement a custom function similar to sizeof operator in C . As shown in below code I have a function which is returning size for int datatype.
How can I convert this function into generalize way so it can accept any datatype and return size for passed data type.
#include<stdio.h>
int sizeof_fun(int a)
{
return (char *)(&a+1) - (char *)(&a);
}
int main()
{
int test;
printf("Sizeof int -> %d \n", sizeof_fun(test));
return 0;
}
You cannot do this as a C function.
C function parameters are fixed, and thus you can't have a that dynamically returns different sizes based on the type of parameter. Once inside the function, you don't know the type of the original variable holding the parameter. You would need to either use:
The real sizeof (why do you need to reimplement that? you can't!)
Preprocessor macros
C++ generics/templates
Function overloading (again, C++)

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.

Is passing pointers to functions similar to calling a function by reference? [duplicate]

This question already has answers here:
Passing by reference in C
(19 answers)
Closed 4 years ago.
I don't really see any difference, with passing pointers to functions and calling a function by reference. Am I right
#include <stdio.h>
int multi;
int multiplication(int *a, int *b){
multi = (*a) * (*b);
return multi;
}
int main()
{
int X = 2, Y=3;
multiplication(&X, &Y);
printf("%d", multi);
return 0;
}
From the example code you show, you obviously mean pass by reference, not call by reference.
There is no pass by reference in C, it's always pass by value. Of course, you can get the effect of pass by reference by passing a pointer to something. The pointer is your reference. It's again passed by value, but you use that value to access some other object.
Languages that have pass by reference will typically use pointers to implement it.

What does it mean when a C function pointer has a void parameter? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C void arguments
I am looking at some OpenGL graphics code and it has the following:
glutIdleFunc(void(*func)(void));
When does it mean to have a function pointer with a void argument in C? Does this mean the function can take in any arguments or is not allow to take in any arguments, or something else?
It means you have to pass a pointer to a function that has no parameters and returns nothing.
void func(void)
is a function that takes no parameters and does not return anything.
This is not to be confused with:
void func()
which in C (not C++) is a function that has no parameter checking, and does not return anything.
This is not to be confused with:
func(void)
which is a function which takes no parameters and returns an int, by default.

Resources