3

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.

SQLite C Interface - One-Step Query Execution Interface

14
  • I'm using Arduino IDE and we can mix C and CPP files. Commented Jun 2, 2023 at 6:35
  • 1
    There is no such thing as C/CPP. String is not a standard C++ type. In C++ we avoid C-style arrays, raw pointers, void pointers, and casts. String* [100][10] is a 100x10 array of pointers to String. If you need a pointer to an array, the best option is to use typedef. Commented Jun 2, 2023 at 6:41
  • @Jason There is nothing in either of those so-called 'duplicates' about casting to a 2D array pointer. I shall reopen this question... Commented Jun 2, 2023 at 6:56
  • 2
    @AdrianMole Just including "casting" in the title of the question doesn't make the question about casting. Commented Jun 2, 2023 at 7:00
  • 2
    @AdrianMole There is nothing to assume here. Just because the title contains the word "casting" doesn't mean the question has to do something with casting or that the problem is related to casting. The duplicates solves OP's problem. Commented Jun 2, 2023 at 7:02

2 Answers 2

5

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.

10
  • You're right, I confused an "array of pointers" and a "pointer to an array" syntax. Maybe I need to sleep a bit more ! Thanks Commented Jun 2, 2023 at 7:21
  • Just one minor nit-pick: This question is tagged as C++, so why the C-style cast? Commented Jun 2, 2023 at 14:17
  • @AdrianMole This is an issue, I updated it. The sqlite library I'm using is well in C language. I'm working on Arduino projects and there are cpp and c files libraries. Sometimes, I get mixed up a bit. Commented Jun 3, 2023 at 10:20
  • @ManWithNoName You changed the language tag after answers have been posted - thus invalidating my answer. Also, your cited error message is still (quite clearly) from a C++ (not C) compiler. And the Arduino compiler (IIRC) works as a C++ compiler. Commented Jun 3, 2023 at 10:46
  • @AdrianMole OK, thanks for the info. Arduino IDE is a bit a mystery for me. Before, I worked on MPLAB IDE in ASM and C. Having the possibility to work in C++ with object on microcontrollers is new for me ! Commented Jun 3, 2023 at 12:40
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.

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.

2
  • Although the OP says that he wants a pointer to a 2D array, and that's what you have above. I think that's likely a misunderstanding on the OPs part. Probably they just want some code that works, and there is an extra level of indirection than is necessary in the code above.
    – john
    Commented Jun 2, 2023 at 6:56
  • @john Yes, indeed. I did originally mean to reference your answer in the "better design solutions" part of my post (which I have now done). Commented Jun 2, 2023 at 7:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.