0

Lets say during compilation, I know I have 3 or n char *cmd[] arrays. I need to store it somewhere How do I store 3 or n char *cmd[] arrays?

Like storing lists of list in java. I could store 3 or n and then go over them in a loop as it is dynamic enough to allow me to add any number of lists to it

Sorry, I am new to c so don't quite understand pointer storage

Here's what I am trying to do:

char **cmds[len+1]; //store all command arrays
int c = 0;
int pipe_c=0;

int start = 0, end=0;
len = 3 (here)
for(int i=0; i<len; i++){ //iterate over all set of pipe arugments (x y z | x y | d)
    end = pipeIndices[i]; //end index of token to copy (excluding)
    char *cmd[end-start+1]; //create an array to store first set of tokens (x y z)
    for(start; start<end; start++){
        cmd[c++] = strdup(tokens[start]); //copy tokens from start to end
    }
    start = end+1; //next set of tokens 
    cmds[pipe_c++] = cmd; //add cmd array to cmds
}
Learner
  • 61
  • 1
  • 7
  • Store *where* exactly? – Marco Bonelli Feb 28 '20 at 01:41
  • @MarcoBonelli That's what the question is..Like how can I keep track of those 3 arrays and use them? Like is there a data structure like vector or something that could help with that? – Learner Feb 28 '20 at 01:42
  • `char *cmd[]` is an *array of an unknown number of pointers to char* (it is an incomplete type on its own). `cmd` must have been declared and initialized somewhere else. In order to store anything at one of the pointers in `cmd`, your character must reside in valid storage elsewhere and you assign the address of where the character is stored to your array. If you really have to keep track of 3-arrays of pointers to char, consider a `struct` where each array is a member. That way all 3 can be accessed through a single pointer. – David C. Rankin Feb 28 '20 at 01:42
  • 1
    I don't really get your problem. Declare three arrays and there you go, you surely can keep track of them by their name. Your question is way too vague. – Marco Bonelli Feb 28 '20 at 01:44
  • And as @MarcoBonelli states, the question is a bit unclear. Have a look at [**How to Ask a Question**](http://stackoverflow.com/questions/how-to-ask) and [**How to create a Minimal, Complete, and Verifiable example**](http://stackoverflow.com/help/mcve). – David C. Rankin Feb 28 '20 at 01:45
  • @MarcoBonelli It could be more than 3... it could be n and I need to go over them in a loop, that's like storing Lists of lists in java – Learner Feb 28 '20 at 01:45
  • @DavidC.Rankin It is like I am copying over some tokens into each array and then iterate over those arrays but the arrays could be 3 or 4 so i need some dynamic way to store those arrays so that I can go through them in a loop in my other functions – Learner Feb 28 '20 at 01:48
  • 1
    Here is my best **guess** at what you want to do: `char *cmd1[] = { "do", "this" }, *cmd2[] = { "and", "that" }, *cmd3[] = { "over", "again" };` then to coordinate all three `char **all[] = { cmd1, cmd2, cmd3 };` Then you can nest an `i, j` loop and access each as `all[i][j]`. – David C. Rankin Feb 28 '20 at 01:55
  • @DavidC.Rankin yes, I think that would work. Let me try that. – Learner Feb 28 '20 at 01:57
  • @DavidC.Rankin I don't know why it gives garbage values when I try to copy over the strings .. – Learner Feb 28 '20 at 02:13
  • My comment uses *string-literals* which have their own storage already created. You can't simply copy to `all[i][j]` without allocating storage first, e.g. `all[i][j] = malloc (strlen (str_to_store) + 1);` (validate by checking return), then you can copy `str_to_store` to `all[i][j]`. See [Pointer to Pointer to Array](https://susepaste.org/60533522) for my example using string literals. – David C. Rankin Feb 28 '20 at 02:16
  • @DavidC.Rankin Can it work for this code char **cmds[len+1]; //store all command arrays int c = 0; int pipe_c=0; int start = 0, end=0; len = 3 (here) for(int i=0; i – Learner Feb 28 '20 at 02:30
  • You have a VLA of `len+1` *pointers to array of pointers to char* (and `len` would have to be defined *before* you declare `char **cmds[len+1];`. Using `strdup` is a good way to allocate for each string. You are heading in the right direction. I would suggest working it a bit further, then asking a new question on whatever specific part you get stuck on. – David C. Rankin Feb 28 '20 at 02:35
  • @DavidC.Rankin is it required to assign memory before the assignment of char*array[] to char**array[] for doing something like cmds[c] = cmd where cmds is char** array[] and cmd is char* array[] – Learner Feb 28 '20 at 02:56
  • Recall `char **cmds[len+1];` a declares `len+1` pointers to an array of pointers to char. You have `len + 1` uninitialized pointers (they point nowhere until assigned). What you assign to each pointer, and either be newly allocated memory, or the address of an existing array of pointers. For example, in my example the array of pointers assigned to each pointer in `all`, were previously declared with *automatic storage duration* `char *cmd1[] = { "do", "this" }`. So `cmd1` and each pointer in it already points to valid storage. Know what each pointer points to `:)` – David C. Rankin Feb 28 '20 at 03:06
  • @DavidC.Rankin I am sorry but I don't get it. For char *, it made sense to allocate memory for strings, then we assign it to char *arr[index] = starredstring and then we assign the prepared array to char**array[] as char ** array[index] = *arr. But on doing something like array[0][0], there's segmentation fault. – Learner Feb 28 '20 at 03:16
  • 1
    Here is a previous answer where I walk through allocating for groups of strings that is probably the best I can do in a comment [How do you continue to scan until new line C without using (fgets)](https://stackoverflow.com/questions/60198370/how-do-you-continue-to-scan-until-new-line-c-without-using-fgets/60200279#60200279) – David C. Rankin Feb 28 '20 at 03:19
  • @DavidC.Rankin thank you so much. The link helped to understand 3 pointer storage allocation and I used char*** to store char** arguments which I was so confused about – Learner Feb 28 '20 at 21:13
  • 1
    Glad it helped. Once you get your head wrapped around allocating for blocks of memory holding pointers, and then allocating blocks of memory for each individual object you have and that you assign the beginning address for each of those blocks to one of the pointers you have allocated, in sequence, it's all the same no matter how many levels of indirection you have. (but it does help seeing all put together to help the pieces fall into place in your mind...) Good luck with your coding! – David C. Rankin Feb 28 '20 at 21:44

0 Answers0