2D数组作为参数
2D arrays as arguments
我读过一本关于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 */
}
}
相关文章:
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- C++中的高效循环缓冲区,它将被传递给C样式数组函数参数
- 如何使用Visual Studio 2017在C++中为参数化对象数组使用唯一指针
- 为 NewObjectA() 函数创建 jvalues 的参数数组时出错 - JNI Invocation API
- 估计函数参数数组的维度
- 温度参数数组的内存泄漏
- 如何将值附加到命令行参数数组?
- libpqxx:如何绑定参数数组
- 如何在德尔福中C++参数数组指针算术
- C++函数参数或参数数组
- 将 std::function 代理为需要参数数组的 C 函数
- 分析字符串参数数组
- Pro*C传递参数数组
- OpenCV错误:cvGetSize中的错误参数(数组应该是CvMat或IplImage)
- c++无限参数数组
- 查找命令行参数数组中的元素数
- 如何将目录中所有具有完整路径的文件传递到execv中的参数数组
- ODBC:参数数组-忽略参数
- 使用lambda参数数组