Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass in an array of integer pointers

Tags:

c

my function:

int f1(int* r1, int* r2, int* r3, int* r4) {

    *r1 = 1;
    *r2 = 343;
    *r3 = 34;
    *r4 = 3;

    return c; // c = 1 if success
}

caller:
f1 (&r1, &r2, &r3, &r4);

if I want to simplify my function, should I pass in an array of four int pointers? or should i do it with an array of four int ?

int f1(int* r1[4])?

or

int f1(int r1[4])?

Thank you.

like image 606
michael Avatar asked Dec 06 '25 15:12

michael


1 Answers

Since arrays decay into pointers when passed to a function, an array would do:

void f(int *p)
{
     p[0] = 1;
     p[1] = 2;
     p[2] = 3;
     p[3] = 4;
}

int arr[] = { 0, 0, 0, 0 };
f(arr);

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!