0

Say I have a 3-dimensional char array

char strList[CONST_A][CONST_B][CONST_C];

And an array pointer (changed after comments pointed out the error):

char * args[CONST_C];

I want to pick a part of strList and make args to be that value. For example, if strList representing something like

{{"Your", "question", "is", "ready", "to", "publish!"},
 {"Our", "automated", "system", "checked", "for", "ways", "to", "improve", "your", "question"},
 {"and", "found", "none."}}

and I want args to be

{"and", "found", "none."}

how should I do it?

I've tried to use args = strlist[someIndex]; but got an error saying incompatible types when assigning to type ‘char *[100]’ from type ‘char (*)[100]’ , strcpy also seems to fail (probably due to args was not allocated with enough space?), what should I do to properly assign args?

edit: args has been used prior to the assignment, so changing the type of args, while plausible, does require a lot of extra work on other parts of the code.

1
  • 1
    char * args[CONST_C]; this is not a pointer to array, this is an array of pointers Commented Apr 15, 2021 at 20:56

2 Answers 2

1

What you have now is an array of pointers, a better option would be a pointer to array:

Live demo

#include <stdio.h>

#define CONST_A 5
#define CONST_B 10
#define CONST_C 15

int main(void) {
  char strList[CONST_A][CONST_B][CONST_C] = {
      {"Your", "question", "is", "ready", "to", "publish!"},
      {"Our", "automated", "system", "checked", "for", "ways", "to", "improve",
       "your", "question"},
      {"and", "found", "none."}};

  char(*args)[CONST_C] = strList[2];

  for (size_t i = 0; i < 3; i++) {
    printf("%s ", args[i]);
  }
}

Output:

and found none. 

If you want to use your original array of pointers, you can also do it, but the assignments would also have to be manual sort of speak, you would need to iterate over the arrays to make the assigments, something like:

Live demo

char *args[3];

for (int i = 0; i < 3; i++)
{
    args[i] = strList[2][i];
}

Like you asked in the comments, you can have a single char pointer pointing to one of the strings, like for instance:

char *args = strList[2][0]; // and
args = strList[2][1];       // found
...

But iterating over the pointer like in the first example is not possible, the dimension of the array is necessary for a correct iteration, hence the need for a pointer to array.

Sign up to request clarification or add additional context in comments.

3 Comments

Is there a way to do this without modifying the type of ptr? In my case, the ptr was declared as char * prt and a lot of code has been using this, changing it to char (*ptr) would require extra works on the rest of the code. Thank you.
@AmarthGûl, it's perfectly possible, char *ptr = strList[2][0]; will point to and, ptr = strList[2][1] will point to found, etc, but to iterate in a for loop like I have in my code, using the pointer, a pointer to array is needed.
@AmarthGûl You can always use the original array of pointers solution, is not as natural as the first, but it 's certainly better than a char pointer. I added that to my answer.
1

You could use a pointer to an array:

char (*args)[CONST_C] = strList[2];

Now code:

    puts(args[0]);
    puts(args[1]);
    puts(args[2]);

will produce:

and
found
none.

Comments

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.