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,749
  • 6
  • 46
  • 77
Josh Morrison
  • 7,488
  • 25
  • 67
  • 86

3 Answers3

5

This should work fine:

void foo(char (*c)[10]);
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
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
  • 22,034
  • 5
  • 72
  • 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
  • 730,956
  • 141
  • 904
  • 1,278