How can you use this function pointer declaration?
int (* get_function(char c)) (int, int);
I have three functions int function_a(int a, int b), int function_b(int a, int b) and int function_c(int a, int b). I want to use the above function pointer to call one of my functions conditionally based on c.
Here is an example:
#include <stdio.h>
int function_a(int a, int b)
{
printf("Inside function_a: %d %d\n", a, b);
return a+b;
}
int function_b(int a, int b)
{
printf("Inside function_b: %d %d\n", a, b);
return a+b;
}
int function_c(int a, int b)
{
printf("Inside function_c: %d %d\n", a, b);
return a+b;
}
int function_whatever(int a, int b)
{
printf("Inside function_whatever: %d %d\n", a, b);
return a+b;
}
int (* get_function(char c)) (int, int)
{
switch(c)
{
case 'A':
return function_a;
case 'B':
return function_b;
case 'C':
return function_c;
}
return function_whatever;
}
int main(void) {
get_function('B')(3, 5);
return 0;
}
get_function('B') returns a function pointer to function_b and get_function('B')(3, 5); also calls that function.
https://ideone.com/0kUp47
Related
here is my code, when the input is 21,15 I got the output is 0. what I expected is 3. the return value of the function divisor seems return a wrong value.
#include<stdio.h>
int divisor(int a, int b){
//when b is 0, got the GCD
if(b==0){
printf("when b is 0, a=%d\n", a);
return a;
}
else{
//recursion
printf("the input a=%d,b=%d\n", b, a%b);
divisor(b, a%b);
}
// return res;
}
int main(void){
int a, b;
scanf("%d,%d", &a, &b);
int r = divisor(a, b);
printf("%d", r);
return 0;
}
You can try this, but it will only work for positive integers.
int divisor(int a, int b){
if (a * b == 0){
return a+b;
}
return divisor(b % a, a % b);
}
This question already has answers here:
How do I return an array of struct from a function?
(3 answers)
Closed 2 years ago.
I have a function that prints out 11 points of a quadratic function ax^2 + bx + c with a, b, c as input. The function works fine except I need to use structures, not just variables x and y. How can I get my function to return a structure value and store it in a structure array then print out the structure array?
struct point {
int x;
int y;
};
struct point *findpoint(int a, int b, int c){
int i, x, y;
x = -5;
for (i = 0; i < 11; i++)
{
y = (a * (x * x)+ (b * x) + c);
printf("The points are {%d, %d}\n", x, y);
x++;
}
}
struct point arr_point[11];
int main(int argc, char *argv[]){
struct point *ptr;
printf("Enter coefficients a, b, c:");
int a, b, c;
int i;
for (i = 0; i < argc; i++){
scanf("%d %d %d", &a, &b, &c);
}
printf("%d %d %d\n", a, b, c);
findpoint(a, b, c);
return 0;
}
You can create an alias for your struct with typedef:
typedef struct {
int x;
int y;
} Foo_t;
Foo_t returnStruct() {
Foo_t foo = {1,2};
return foo;
}
int main() {
const uint8_t SIZE_ARRAY = 2;
Foo_t arrayFoo[SIZE_ARRAY];
arrayFoo[0] = returnStruct();
arrayFoo[1] = returnStruct();
for(uint8_t i=0;i<SIZE_ARRAY;i++) {
printf("Index %d: x=%d y=%d \n", i, arrayFoo[i].x, arrayFoo[i].y);
}
}
So, I'm trying to put the output of one function and put it to another function
Here are the functions that I'm trying to get output and inputs, you can just ignore inside of a second function I just simply put printf to check if the variables are correct.
int guess(int a, int b){
printf("\nEnter you guess: ");
scanf("%d,%d", &a, &b);
return a, b;
}
int check(int a, int b){
printf("%d %d ",a,b);
}
And here is the code:
#include <stdio.h>
int main(){
int row, column;
guess(row, column);
check(row, column);
}
int guess(int a, int b){
printf("\nEnter you guess: ");
scanf("%d,%d", &a, &b);
return a, b;
}
int check(int a, int b){
printf("%d %d ",a,b);
}
I tried to put it simply to understand how to do it more clearly.
When I run the code and put coordinates for example: 4,5 and it only prints out 0 1
Also, Is it possible to do it with arrays?
1) You need to use a prototype or declare your functions forward.
2) You can not return 2 variables form a function in C, but you can pass an array and read/write his values:
#include <stdio.h>
void guess(int arr[])
{
printf("\nEnter you guess: ");
scanf("%d,%d", &arr[0], &arr[1]);
}
void check(int arr[])
{
printf("%d %d ",arr[0], arr[1]);
}
int main(void)
{
int arr[2];
guess(arr);
check(arr);
return 0;
}
or you can pass a reference
void guess(int *a, int *b)
{
printf("\nEnter you guess: ");
scanf("%d,%d", a, b);
}
guess(&a, &b);
The input numbers you read in guess functions are actually only read into the local variables a and b. You'd need to pass pointers to be able to read into the vars in main.
Also there's no way to return multiple values from a function in C.
#include <stdio.h>
void guess(int *a, int *b)
{
printf("\nEnter you guess: ");
scanf("%d,%d", a, b);
}
int check(int a, int b)
{
printf("%d %d " ,a, b);
}
int main()
{
int row = 0, column = 0;
guess(&row, &column);
check(row, column);
}
You should also check the return value of scanf for failures.
I am trying to pause my screen to test code but i don't know were to put system("pause") anywhere I put it says undefined
#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS
void load(int*a, int*b, int*c)
{
printf("Enter 3 numbers");
scanf("%d %d %d", &(*a), &(*b), &*(c));
}
void calc(int a, int b, int c, int *sum, float *avg)
{
*sum = a + b + c;
*avg = *sum / (float)3;
}
void print(int a, int b, int c, int sum, float avg)
{
printf("The 3 numbers are%d %d %d \n",a, b, c);
printf("The sum is %d\n", sum);
printf("The Avg is %f\n", avg);
}
void main()
{
int a, b, c, sum;
float avg;
load(&a, &b, &c);
calc(a, b, c, &sum, &avg);
print(a, b, c, sum,avg);
}
Put it in the end of main().
And donnot forget to include its header:
#include <stdlib.h>
i have code to array of func pointer
#include <stdio.h>
int sum(int a, int b);
int subtract(int a, int b);
int mul(int a, int b);
int div(int a, int b);
int (*p[4]) (int x, int y);
int main(void)
{
int result;
int i, j, op;
p[0] = sum; /* address of sum() */
p[1] = subtract; /* address of subtract() */
p[2] = mul; /* address of mul() */
p[3] = div; /* address of div() */
printf("Enter two numbers: ");
scanf("%d %d", &i, &j);
printf("0: Add, 1: Subtract, 2: Multiply, 3: Divide\n");
do {
printf("Enter number of operation: ");
scanf("%d", &op);
} while(op<0 || op>3);
result = (*p[op]) (i, j);
printf("%d", result);
return 0;
}
int sum(int a, int b)
{
return a + b;
}
int subtract(int a, int b)
{
return a - b;
}
int mul(int a, int b)
{
return a * b;
}
int div(int a, int b)
{
if(b)
return a / b;
else
return 0;
}
code for array of pointer to function:
#include <stdio.h>
int sum(int, int);
int product(int, int);
int subtract(int, int);
int main()
{
int i = 0;
int a = 10;
int b = 5;
int result = 0;
int (*pfun[3])(int, int);
pfun[0] = sum;
pfun[1] = product;
pfun[2] = subtract;
for( i = 0 ; i < 3 ; i++)
{
result = pfun[i](a, b);
printf("\nresult = %d", result);
}
result = pfun[1](pfun[0](a, b), pfun[2](a, b));
printf("\n\nThe product of the sum and the subtract = %d\n",result);
}
int sum(int x, int y)
{
return x + y;
}
int product(int x, int y)
{
return x * y;
}
int subtract(int x, int y)
{
return x - y;
}
now how to combine this two program. such that array of pointers pointing to func pointers and the func pointers may have different number of args? any suggestion.
You not only need to store function pointers with a variable number of arguments (that is not very difficult, you could use a union for instance), but you also need to make sure you call the functions with the correct argument, and that is a bit trickier given your design.
I suggest to use a stack instead. All your functions would only take the stack as an argument:
void sum(stack_t *stack);
void subtract(stack_t *stack);
void product(stack_t *stack);
And your array could be declared this way:
typedef void callback_t(stack_t *);
callback_t *p[] =
{
sum,
subtract,
product,
/* ... */
};
Then for instance sum would be implemented as such:
void sum(stack_t *stack)
{
if (depth(stack) < 2)
perror("Not enough arguments in stack!");
int b = popstack(stack);
int a = popstack(stack);
int c = a + b;
pushstack(stack, c);
}
But unary minus would be implemented this way:
void neg(stack_t *stack)
{
if (depth(stack) < 1)
perror("Not enough arguments in stack!");
int a = popstack(stack);
pushstack(stack, -a);
}
Each function decides how many arguments they need. The caller does not need to know.