将二维字符数组传递给函数

时间:2015-02-12 04:22:29

标签: c arrays

我正在尝试创建一个程序来获取二维数组并自行打印每一行(如果您曾在Codecademy中学过Python课程,那就像战舰课程一样,我正在尝试打印出来板)。我做了很多谷歌搜索,但二维字符阵列似乎没有在互联网上得到很好的覆盖

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const int n = 5;
void printgraph(char *graph[]) {
    printf("\nPrinting graph:\n\n");
    int i = 0;
    for(i = 0; i <= 5 - 1; i++) {   // this is only a temporary solution
        printf("i %i | %s\n", i, &graph[i]);    // error here
    }
}

    int main(int argc, char *argv[]) {
    char array[5][6];    // defining the char array here
    strcpy(array[0], "abcde");
    strcpy(array[1], "fghij");
    strcpy(array[2], "klmno");
    strcpy(array[3], "pqrst");
    strcpy(array[4], "uvwxy");
 printf("array: %s\n", array[1]);   // it prints out perfectly within the function itself
    printgraph(*array);    // error here
    return 0;
}

终端告诉我这是

$cc -g -Wall ex3.c -o ex3
ex3.c: In function ‘printgraph’:
ex3.c:11:3: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘char **’ [-Wformat=]
   printf("i %i | %s\n", i, &graph[i]);
   ^
ex3.c: In function ‘main’:
ex3.c:23:2: warning: passing argument 1 of ‘printgraph’ from incompatible pointer type [enabled by default]
  printgraph(*array);
  ^
ex3.c:6:6: note: expected ‘char **’ but argument is of type ‘char *’
 void printgraph(char *graph[]) {
      ^
$./ex3
array: fghij

Printing graph:

i 0 | abcde
i 1 | hij
i 2 | o
i 3 | uvwxy
i 4 | 

如您所见,并非所有阵列都正确打印出来。我已经尝试了不同的变化来指出,但这一个是唯一一个正常工作的人

3 个答案:

答案 0 :(得分:4)

为什么不将整个数组传递给函数并在函数内打印出所需的东西。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const int n = 5;
void printgraph(char graph[][6]) { /* ---> Changes made here */
    printf("\nPrinting graph:\n\n");
    int i = 0;
    for(i = 0; i <= 5 - 1; i++) {   
        printf("i %i | %s\n", i, graph[i]);   /* Check the changes here */
    }
}

int main(int argc, char *argv[]) {
char array[5][6];    // defining the char array here
strcpy(array[0], "abcde");
strcpy(array[1], "fghij");
strcpy(array[2], "klmno");
strcpy(array[3], "pqrst");
strcpy(array[4], "uvwxy");
printgraph(array);    /* Pass the array to the function */
return 0;
}

答案 1 :(得分:1)

一个简单的解决方法可能是在char中将main()的双指针声明为数组,并将双指针传递给printgraph()函数。

代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const int n = 5;
void printgraph(char **graph) {
    printf("\nPrinting graph:\n\n");
    for(int i = 0; i < 5; i++) {   // this is only a temporary solution
        printf("i %i | %s\n", i, graph[i]);   
    }
}

int main(int argc, char *argv[]) {
    char *array[5];
    for(int i=0;i<5;++i)
        array[i] = malloc(6);

    strcpy(array[0], "abcde");
    strcpy(array[1], "fghij");
    strcpy(array[2], "klmno");
    strcpy(array[3], "pqrst");
    strcpy(array[4], "uvwxy");
    printf("array: %s\n", array[1]);    

    printgraph(array); 

    for(int i=0;i<5;++i)
        free(array[i]);

    return 0;
}

答案 2 :(得分:0)

当您将一次取消引用的数组作为参数传递(即第一个数组的地址)时,您只能打印第一个数组。

你可以在下面看到,我们可以利用指向6个元素数组的指针来收集2D数组的地址,其中每个1D数组有6个元素。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const int n = 5;
void printgraph(char (*graph)[6]) //pointer to array of 6 elements//
    {
        printf("\nPrinting graph:\n\n");
        int i = 0;
        for(i = 0; i <= 5 - 1; i++) {   // this is only a temporary solution
            printf("i %i | %s\n", i, graph[i]);    // error here
    }
}

int main(int argc, char *argv[])
{
    char array[5][6];    // defining the char array here
    printf("%u\n",array);
    printf("%u\n",&array);
    strcpy(array[0], "abcde");
    strcpy(array[1], "fghij");
    strcpy(array[2], "klmno");
    strcpy(array[3], "pqrst");
    strcpy(array[4], "uvwxy");
    printf("array: %s\n", array[1]);   // it prints out perfectly within the function itself
    printgraph(array);    // error here
    return 0;
}

找到输出:

array: fghij

Printing graph:

i 0 | abcde
i 1 | fghij
i 2 | klmno
i 3 | pqrst
i 4 | uvwxy