2D数组作为参数

2D arrays as arguments

本文关键字:参数 数组 2D      更新时间:2023-10-16

我读过一本关于C的书,我确实试过了,所以请对我宽容一点。我正在努力理解记忆是如何运作的。

我想要一些像这样的单词数组(在C中):

char builts[20][10]={"light","temp"}; //should it looks like this ?

然后,我想将该数组传递给函数(在另一个类中)

   //some class
    char  *builtinFunctions;
    void Intepreter::setBuiltIns( char *builtins)
    {
      //  here - should I save a copy to builtinFunctions ?? how ?
    }

另一个类需要始终访问该单词数组。为什么这是给出错误:intepreter.setBuiltIns(&builts);

如何声明builtinFunctions?作为pointer还是array?应该将其复制到吗?

整件事到底应该是什么样子??

有多种方法可以将2D数组传递给函数:

参数是2D阵列

int array[10][10];
void passFunc(int a[][10])
{
    // ...
}
passFunc(array);

参数是一个包含指针的数组

int *array[10];
for(int i = 0; i < 10; i++)
    array[i] = (int*)malloc(40); //array[i] = new int[10];
void passFunc(int *a[10]) //Array containing pointers
{
    // ...
}
passFunc(array);

参数是指向的指针

int **array;
array = (int*)malloc(40);//array = new int *[10];
for(int i = 0; i <10; i++)
    array[i] = (int*)malloc(40); //array[i] = new int[10];
void passFunc(int **a)
{
    // ...
}
passFunc(array);

将简单数组传递给函数时,将其作为指针传递(数组名称衰减为指向数组第一个元素的指针),如下所示:

int foo[3] = { 3, 2, 1 };
bar(foo);

因此,您的函数将指向int的指针作为参数:

void bar(int *data) { }

这里有一个数组,它包含长度为10的以NULL结尾的字符串,这些字符串也是数组。因此,builts是指向他的第一个元素的指针,因此是指向10个char:的数组的指针

char builts[20][10] = {"light", "temp"};
char (*foo)[10] = builts; // This is valid, foo is a pointer to an array of 10 char

因此,您的函数必须采用char (*)[10]类型的参数,因为您传递了一个指向10个char:数组的指针

void bar(char (*data)[10]) { }

您可以在调用函数时仅使用其名称来传递2天数组,如:

test_2d(builts);

您可以将单词总数作为单独的参数传递,也可以制作一个特殊的单词来指示数组的末尾。例如,我使用了一个特殊的单词""来表示这标志着数组的结束。总的来说,代码看起来像。

#include<stdio.h>
#include<string.h>
int main(void)
{
    char builts[20][10]={"light","temp", ""};
    test_2d(builts);        /* without passing total number of args; depends on special word at the end */
    test_2d_num(builts, 2); /* also pass total num of elements as argument */
    return 0;
}
/* This needs a special word at the end to indicate the end */
void test_2d(char builts[][10])
{
    int i;
    char tmp[10];
    /* just print the words from word array */
    for (i=0; *builts[i] != ''; i++ )
            printf("%sn", builts[i]);
    /* also try copy words to a tmp word and print */
    for (i=0; *builts[i] != ''; i++ ) { 
            strcpy(tmp, builts[i]);
            printf("%sn", tmp);
    }
    /* Do something */
}
/* Also get total number of elements as a parameter */
void test_2d_num(char builts[][10], int tot_elem)
{
    int i;
    /* Process each word from the array */
    for (i = 0; i < tot_elem; i++) {
         printf("%sn", builts[i]);
         /* Do something */
    }
}

请注意,此函数只能处理像builts[][10]这样的数组,而不能处理builts[][11]builts[][9]

如果您想要一个泛型函数,那么您需要将单个单词的地址存储在char *arr[]中,并将此数组传递给该函数。像

int main()
{
     /* store the addresses of individual words in an `char *arr[] */
     char *arr[] = {"hello", "this", "that", NULL};
     test_2d_gen(arr);
     return 0;
}
void test_2d_gen(char *arr[])
{
     int i;
     /* process each word */
     for (i = 0; arr[i] != NULL; i++) {
         printf("%sn", arr[i]);
     /* Do something */
     }
}