英文:
Casting a void pointer to a 2D String array pointer (C/CPP)
问题
我正在使用一个需要一个带有 void*
指针作为参数的函数库。我有一个二维字符串数组,并想通过该参数将该数组传递并在函数内提取它。我成功地将数组传递为指针,但不知道如何将该指针转换回我的数组。
这是我的当前代码:
String str_array[100][10];
int callback(void* data) {
String (*str_array_ptr)[100][10] = (String (*)[100][10])data;
(*str_array_ptr)[0][0] = "text";
return 0;
}
void test() {
callback(&str_array);
}
然而,在编译时,我获得以下错误消息:
error: ISO C++ forbids casting to an array type 'String* [100][10]' [-fpermissive]
PS: 我正在尝试使用 SQLite 库的 sqlite3_exec()
函数,并将 "SELECT SQL query" 的结果存储到一个二维字符串数组中。
SQLite C Interface - One-Step Query Execution Interface
英文:
I'm using a library which requires a function with a void*
pointer as a parameter. I have a 2D string array and I want to pass that array through that parameter and extract it inside the function. I successfully passed the array as a pointer but I don't know how to convert that pointer back to my array.
This is my current code:
String str_array[100][10];
int callback(void* data) {
String* str_array_ptr[100][10] = (String* [100][10])data;
(*str_array_ptr)[0][0] = "text";
return 0;
}
void test() {
callback(&str_array);
}
However, when compiling, I obtain the following error message:
> error: ISO C++ forbids casting to an array type 'String* [100][10]' [-fpermissive]
PS: I'm trying to use the SQLite library's sqlite3_exec()
function and store the result of a "SELECT SQL query" into a 2D string array.
答案1
得分: 5
不能将指针转换为数组。相反,您通过另一个指针访问数组。该指针的类型为 String (*)[10]
。像这样:
String str_array[100][10];
int callback(void* data) {
String (*str_array_ptr)[10] = (String (*)[10])data;
str_array_ptr[0][0] = "text"; // 注意没有 '*'
return 0;
}
void test() {
callback(str_array); // 注意没有 '&'
}
在创建指针的方式和访问指针的方式上,都不需要使用 &
或 *
,在您的代码中使用它们都是错误的。请参考上面的代码了解详细信息。
这里的根本问题(也许是您误解的问题)在于 String *x[10];
和 String (*x)[10];
之间的区别。在第一种情况下,x
是一个包含 10 个指向 String
的指针的数组,在第二种情况下,x
是指向包含十个 String
的数组的指针。您需要的是第二种选项。
英文:
You cannot cast a pointer to an array. Instead you access your array through another pointer. That pointer has type String (*)[10]
. Like this
String str_array[100][10];
int callback(void* data) {
String (*str_array_ptr)[10] = (String (*)[10])data;
str_array_ptr[0][0] = "text"; // Note no '*'
return 0;
}
void test() {
callback(str_array); // Note no '&'
}
Both the way you create the pointer, you don't need to use &
, and the way you access the pointer, you don't need to use *
, are also wrong in your code. See the code above for details.
The fundamental issue here (and maybe the issue you are misunderstanding) is the difference between String *x[10];
and String (*x)[10];
. In the first case x
is an array of 10 pointers to String
, in the second case x
is a pointer to an array of ten String
. It's the second option that you want.
答案2
得分: 2
The line, String* str_array_ptr[100][10];
does not declare a pointer to a 2D array of String
objects; rather, it declares a 2D array of pointers to String
objects.
声明行 String* str_array_ptr[100][10];
并不是声明了一个指向 String
对象的二维数组指针;相反,它声明了一个指向 String
对象的指针的二维数组。
The syntax for declaring a pointer to a 2D array is tricky; in your case, it is the following (to declare str_array_ptr
as a pointer to a 100 x 10 array of String
objects):
声明指向二维数组的指针的语法很棘手;在你的情况下,应使用以下方式声明(将 str_array_ptr
声明为指向 100 x 10 的 String
对象数组的指针):
String (*str_array_ptr)[100][10];
Casting to such a pointer is (arguably) even trickier; using a C++ static_cast
(which you can when the source operand is a void*
), you get the following code:
将其转换为此类指针 可能(可以说)更加棘手;使用 C++ 的 static_cast
(当源操作数是 void*
时可以使用),你可以得到以下代码:
#include <iostream>
#include <string>
using String = std::string;
String str_array[100][10];
int callback(void* data) {
String (*str_array_ptr)[100][10] = static_cast<String (*)[100][10]>(data);
(*str_array_ptr)[0][0] = "text";
return 0;
}
void test() {
callback(&str_array);
}
One could argue that, if you really wish to use such a pointer to a 2D array (there are likely better design solutions in C++, such as the answer posted by john), then you could define a type for that pointer with a typedef
or using...
statement (e.g. using StrArrPtrType = decltype(&str_array);
); however, the C++ and C programming communities generally frown (severely and rightly) on hiding pointers behind typedef
or using
aliases.
可以 认为,如果你真的希望使用这样的指向二维数组的指针(在 C++ 中可能有更好的设计解决方案,比如 john 发布的答案),那么你 可以 使用 typedef
或 using...
语句为该指针定义一个类型(例如 using StrArrPtrType = decltype(&str_array);
);然而,C++ 和 C 编程社区通常不赞成(严重且正确地)在 typedef
或 using
别名后隐藏指针。
英文:
The line, String* str_array_ptr[100][10];
does not declare a pointer to a 2D array of String
objects; rather, it declares a 2D array of pointers to String
objects.
The syntax for declaring a pointer to a 2D array is tricky; in your case, it is the following (to declare str_array_ptr
as a pointer to a 100 x 10 array of String
objects):
String (*str_array_ptr)[100][10];
Casting to such a pointer is (arguably) even trickier; using a C++ static_cast
(which you can when the source operand is a void*
), you get the following code:
#include <iostream>
#include <string>
using String = std::string;
String str_array[100][10];
int callback(void* data) {
String (*str_array_ptr)[100][10] = static_cast<String (*)[100][10]>(data);
(*str_array_ptr)[0][0] = "text";
return 0;
}
void test() {
callback(&str_array);
}
One could argue that, if you really wish to use such a pointer to a 2D array (there are likely better design solutions in C++, such as the answer posted by john), then you could define a type for that pointer with a typedef
or using...
statement (e.g. using StrArrPtrType = decltype(&str_array);
); however, the C++ and C programming communities generally frown (severely and rightly) on hiding pointers behind typedef
or using
aliases.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论