kkkkk
kkkkk

Reputation: 716

Multidimensional char array in C in function

How to use function in C with array of strings? My code:

void test(char **a){
    printf("%s", a[0]);
}
int main(){
    char b[10][10];
    strcpy(b[0],"abc");
    strcpy(b[1],"dfgd");
    test(b);
    return 0;
}

How to make this example of code work?

Upvotes: 1

Views: 101

Answers (1)

Ali Akber
Ali Akber

Reputation: 3800

You can use :

void test(char a[10][10]){
    printf("%s", a[0]);
}

or

void test(char a[][10]){
    printf("%s", a[0]);
}

or

void test(char (*a)[10]){
    printf("%s", a[0]);
}

int main(){
    char b[10][10];
    strcpy(b[0],"abc");
    strcpy(b[1],"dfgd");
    test(b);
    return 0;
}

All three declarations are perfectly equivalent. Although last one is better.

This answer explains it better

Upvotes: 4

Related Questions