1

I have this array which is correct, but I need the values and size to be variable. Is this possible? If so, how?

const char *labels[] = { "Group A", "Group B", "Group C", "Group D", "Group E", "Group F", "Group G", "Group H" };

It has to be a const char * array because it is being used as a stringArray parameter, which won't take anything less complicated.

Any help would be appreciated. Please keep in mind that I am a student developer.

2 Answers 2

1

It has to be a const char *array because it is being used as a stringArray parameter, which won't take anything less complicated.

You can pass char* to function expecting const char*.

Given that, maybe you can try something like this:

char**arr = malloc(ARRAY_LEN * sizeof(char*));
for (i=0; i<ARRAY_LEN; i++)
{
     arr[i] = malloc(EACH_STRING_LEN);
     if(arr[i]==NULL) 
        handleError();
     strcpy(arr[i],"test"); // put some string in i-th array
}

and freeing part:

for (i = 0; i < ARRAY_LEN; i++) {
  free(arr[i]);
}
free(arr);
Sign up to request clarification or add additional context in comments.

5 Comments

Op says it has to be a const char *
@Julien-L: see my comment
You right, I had char* const* in mind and then it would have caused problems when putting something in the strings. I +1 you.
This worked, @Giorgi. Thanks! I had to delay working on this part, so sorry for the slow reply.
@NerdMom: glad if it helped.
0

c arrays do not have variable size. If you need this functionality, you have to use a different data structure

What do you mean by "it is being used as a stringArray parameter, which won't take anything less complicated."?

1 Comment

Here is the function that uses it: TextBox *setLabels(StringArray labels) { TextBox *ret = new TextBox(CAxis_setLabels(ptr, labels.data, labels.len)); reg(ret); return ret; }

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.