Parameter passing issue
I am having a problem with passing two dimensional arrays, I get the following error on this code:
connot convert parameter 2 from 'char [49][21]' to 'char *[]'
the code is this:
float xk[MAXITOTAL-1]; // where maxitotal = 50
char block_matl[MAXNALT][21]; // where maxnalt = 50
int itotal;
function definition:
create_colors(float* vals, char* names[], int count);
my call to the function:
create_colors(xk, block_matl, itotal);
Any help would be really appreciated.
void f(char** str, int rows, int cols)
{
for (int i = 0; i < rows; i++)
cout << str << endl;
}
char* r [] = { "ABC", "DEF", "GHI", };
f(r, 3, 4);
Is create_colors your own? Avoid [] wherever possible.
{
for (int i = 0; i < rows; i++)
cout << str << endl;
}
char* r [] = { "ABC", "DEF", "GHI", };
f(r, 3, 4);
Is create_colors your own? Avoid [] wherever possible.
When I changed the definition to:
create_colors(float* vals, char** names, int count);
I now get:
cannot convert parameter 2 from 'char[49][21]' to 'char **'
unfortunately I cannot change the passing parameter from its original format.
When I change the definition to char names[][21] and then pass arrays of second dimension 21 only I get no compiler erros, but if possible I would like to have a higher degree of freedom than this.
create_colors(float* vals, char** names, int count);
I now get:
cannot convert parameter 2 from 'char[49][21]' to 'char **'
unfortunately I cannot change the passing parameter from its original format.
When I change the definition to char names[][21] and then pass arrays of second dimension 21 only I get no compiler erros, but if possible I would like to have a higher degree of freedom than this.
the problem is that a 'multdimensional' array is actually still all contigeous in memory, so you do not have access to a pointer to an array of pointers to arrays of chars. Instead, you just have access to a pointer to a single big array of chars and you are doing pointer arithmetic. You need to declare your array like:
char * strs[numstrs]
so that you actually have a pointer to an array of pointers. You would then be able to fill this array from a 2D array via a loop.
char * strs[numstrs]
so that you actually have a pointer to an array of pointers. You would then be able to fill this array from a 2D array via a loop.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement