How to pass all the parameters of a function into another function in C

2 Answers

0 votes
#include <stdio.h>

typedef struct {
    char ch;
    int n;
    float f;
} Params;

void funcB(Params pr) {
    printf("%c\n", pr.ch);
    printf("%d\n", pr.n);
    printf("%.2f\n", pr.f);
}

void funcA(char ch, int n, float f) {
    Params pr;
    
    pr.ch = ch;
    pr.n = n;
    pr.f = f;

    funcB(pr);
}

int main(void) {
    funcA('z', 8900, 3.14f);
}

  
  
/*
run:

z
8900
3.14

*/

 



answered Aug 27, 2024 by avibootz
edited Aug 28, 2024 by avibootz
0 votes
#include <stdio.h>

typedef struct {
    char ch;
    int n;
    float f;
} Params;

void funcB(Params pr) {
    printf("%c\n", pr.ch);
    printf("%d\n", pr.n);
    printf("%.2f\n", pr.f);
}

void funcA(Params pr) {
    pr.f = pr.f * 2;

    funcB(pr);
}

void func() {
    Params pr = {'z', 8900, 3.14f};
    funcA(pr);
}

int main(void) {
    func();
}

  
  
/*
run:

z
8900
6.28

*/

 



answered Aug 28, 2024 by avibootz
...