I have a two dimensional array of pointers declared in main as
我有一个在main中声明的二维指针数组
char* data[3][8]
which I passed into a function
我把它传递给了一个函数
void func(char*** data)
When I did printf("%p\n", data[0]);
in main
and in the function I got different outputs 0x7ffeabc27640
in main and (nil)
in the function. Albeit printing just data
outputs the same address with that from inside the main
. Why can't I access the array in the function.
当我做printf(“%p \ n”,data [0]);在main和函数中我得到了不同的输出0x7ffeabc27640在main和(nil)函数中。虽然打印只是数据输出与主内部相同的地址。为什么我不能在函数中访问数组。
2 个解决方案
#1
5
If you enable some warnings (which you should always do), you'll get :
如果您启用了一些警告(您应该经常这样做),您将获得:
main.cpp: In function 'main':
main.cpp:6:10: warning: passing argument 1 of 'func' from incompatible pointer type
func(data);
^
main.cpp:2:6: note: expected 'char ***' but argument is of type 'char * (*)[8]'
void func(char*** data) { (void)data; }
^
Which tells you exactly what's wrong, namely that an array is not a pointer. Dereferencing a pointer that has been converted to the wrong type is undefined behaviour, so you can get anything back.
这告诉你到底出了什么问题,即数组不是指针。取消引用已转换为错误类型的指针是未定义的行为,因此您可以返回任何内容。
Have your function take in a char *(*)[8]
if you want to give it a char *(*)[8]
:
如果你想给它一个char *(*)[8],你的函数是否需要char *(*)[8]:
void func(char *(*data)[8]);
Or, if you want to emphasize that data
should point to the first element of an array :
或者,如果您想强调数据应该指向数组的第一个元素:
void func(char *data[][8]);
The two syntaxes are perfectly equivalent.
这两种语法完全等效。
Note : the file is named main.cpp
but is indeed compiled in C mode.
注意:该文件名为main.cpp,但确实是在C模式下编译的。
#2
0
Passing 2D arrays to a function -
将2D数组传递给函数 -
This will help you read-up and better understand how to do this...
这将有助于您阅读并更好地了解如何执行此操作...
http://www.geeksforgeeks.org/pass-2d-array-parameter-c/
The below is a snippet from the web-page - showing one example of how to do this.
以下是网页的摘录 - 显示了如何执行此操作的一个示例。
#include <stdio.h>
const int n = 3;
void print(int arr[][n], int m)
{
int i, j;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
printf("%d ", arr[i][j]);
}
int main()
{
int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
print(arr, 3);
return 0;
}
Output:
1 2 3 4 5 6 7 8 9
#1
5
If you enable some warnings (which you should always do), you'll get :
如果您启用了一些警告(您应该经常这样做),您将获得:
main.cpp: In function 'main':
main.cpp:6:10: warning: passing argument 1 of 'func' from incompatible pointer type
func(data);
^
main.cpp:2:6: note: expected 'char ***' but argument is of type 'char * (*)[8]'
void func(char*** data) { (void)data; }
^
Which tells you exactly what's wrong, namely that an array is not a pointer. Dereferencing a pointer that has been converted to the wrong type is undefined behaviour, so you can get anything back.
这告诉你到底出了什么问题,即数组不是指针。取消引用已转换为错误类型的指针是未定义的行为,因此您可以返回任何内容。
Have your function take in a char *(*)[8]
if you want to give it a char *(*)[8]
:
如果你想给它一个char *(*)[8],你的函数是否需要char *(*)[8]:
void func(char *(*data)[8]);
Or, if you want to emphasize that data
should point to the first element of an array :
或者,如果您想强调数据应该指向数组的第一个元素:
void func(char *data[][8]);
The two syntaxes are perfectly equivalent.
这两种语法完全等效。
Note : the file is named main.cpp
but is indeed compiled in C mode.
注意:该文件名为main.cpp,但确实是在C模式下编译的。
#2
0
Passing 2D arrays to a function -
将2D数组传递给函数 -
This will help you read-up and better understand how to do this...
这将有助于您阅读并更好地了解如何执行此操作...
http://www.geeksforgeeks.org/pass-2d-array-parameter-c/
The below is a snippet from the web-page - showing one example of how to do this.
以下是网页的摘录 - 显示了如何执行此操作的一个示例。
#include <stdio.h>
const int n = 3;
void print(int arr[][n], int m)
{
int i, j;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
printf("%d ", arr[i][j]);
}
int main()
{
int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
print(arr, 3);
return 0;
}
Output:
1 2 3 4 5 6 7 8 9