I'm creating a class were doing something with an array of struct, but the problem is I cannot assign array reference to the object.
already trying to find the solution, but only works with copying the entire array to the object variable with memcpy
, how to do this differently without copying the array?
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define arrayLength(array) (size_t)((sizeof array) / (sizeof array[0]))
typedef struct someStruct {
int id;
char *name;
} someStruct;
class someClass {
private:
someStruct *list;
public:
someClass(someStruct const *const listData) { list = listData; }
};
int main() {
someStruct list[255] = {
{1, "one"},
};
someClass object(list);
return 0;
}
error: invalid conversion from 'const someStruct*' to 'someStruct*' [-fpermissive] someClass(someStruct const *const listData) { list = listData; }
Update: At the time this question was initially asked. I'm still a newbie in learning C/C++. In this context, I'm forcing to how I code in Python to C/C++ without knowing the pointer enough.
You are supplying a const pointer, then assigning a non-const pointer to that const pointer. The compiler is not letting you downgrade const to non-const.
Make someClass's list a const pointer, or remove the const qualifiers from the initializer parameters.