Sign in to follow this  

Parameter passing issue

This topic is 4869 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
void f(char** str, int rows, int cols)
{
for (int i = 0; i < rows; i++)
cout << str[i] << endl;
}

char* r [] = { "ABC", "DEF", "GHI", };
f(r, 3, 4);

Is create_colors your own? Avoid [] wherever possible.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4869 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this