0

When I want to pass a "char (*c)[10];" as a parameter, what argument should I define in my function definition?

I mean, if I have a function:

void foo(*****) { ... }

I want to pass char (*c)[10]; to it, so what do I write in place of *****?

forsvarir
  • 10,499
  • 6
  • 42
  • 75
Josh Morrison
  • 7,288
  • 24
  • 65
  • 85

3 Answers3

5

This should work fine:

void foo(char (*c)[10]);
John Zwinck
  • 223,042
  • 33
  • 293
  • 407
1

You should be able to pass it simply as you're declaring it:

void foo(char (*c)[10]);

And call it as:

foo(c);

This is the best post on this subject: C pointers : pointing to an array of fixed size

Community
  • 1
  • 1
pickypg
  • 21,498
  • 5
  • 68
  • 84
1

Define the function as:

void foo(char (*c)[10])
{
    for (int i = 0; i < sizeof(*c); i++)
        printf("%d: %c\n", i, (*c)[i]);
}

Use the function as:

int main(void)
{
    char a[10] = "abcdefghi";
    char (*c)[10] = &a;
    foo(c);
    return(0);
}
Jonathan Leffler
  • 698,132
  • 130
  • 858
  • 1,229