0

I have a char * that is a long string and I want to create a pointer to a pointer(or pointer array). The char ** is set with the correct memory allocated and I'm trying to parse each word from the from the original string into a char * and place it in the char **.

For example char * text = "fus roh dah char **newtext = (...size allocated) So I'd want to have:

char * t1 = "fus", t2 = "roh", t3 = "dah";
newtext[0] = t1;
newtext[1] = t2;
newtext[2] = t3;

I've tried breaking the original up and making the whitespace into '\0' but I'm still having trouble getting the char * allocated and placed into char**

Jerum
  • 71
  • 2
  • 9
  • show what you have tried – yano Jan 18 '18 at 05:51
  • 3
    Your declarations for `t2` and `t3` are incorrect. The declarators are missing a `*`, so they have type `char`, not `char *`. – Tom Karzes Jan 18 '18 at 05:58
  • Please provide a [mcve] of what you have tried. – Raman Jan 18 '18 at 06:28
  • 1
    A classic example of why the `'*'` should go with the variable and not the type within a declaration. E.g. `char* a, b, c;` does not declare `b` and `c` as `char *`. Using `char *a, b, c;` helps make that more clear. – David C. Rankin Jan 18 '18 at 07:37

2 Answers2

1

Try this char *newtext[n];. Here n is a constant and use this if n is known beforehand.
Otherwise char **newtext = malloc(n * sizeof *newtext); here n is a variable.

Now you can assign char* as in your example:

newtext[0] = t1;
newtext[1] = t2;
newtext[2] = t3;
...
newtext[n-1] = ..;

Hope that helps.

the kamilz
  • 1,860
  • 1
  • 15
  • 19
  • 1
    Avoid answering with **"Try this"** as it implies you are guessing at a proposed solutions (which you are not -- they are correct). E.g., better "*You can use an array of pointers or pointer-to-pointer-to-char*". Only nit would be to use `sizeof` the dereferenced pointer in the call to `malloc`, e.g. `char **newtext = malloc (n * sizeof *newtext);` which will eliminate any potential of an incorrect size. – David C. Rankin Jan 18 '18 at 07:34
  • Thanks for the correction on code, and English is not my native lang. So 'try this' actually mean here "I didn't test it but I'm experienced enough that should work if I didn't screwed up somewhere". Dude even most experienced coders sometimes screws up. – the kamilz Jan 18 '18 at 07:39
  • 1
    No worries, glad to help. – David C. Rankin Jan 18 '18 at 07:39
  • Sorry I was in a rush, I meant to put * after the first, that wasn't the reason why it didn't work. Initially I was just trying to put null characters where the spaces were to separate the words then do t1 = text[0], while t1 isn't null, assign the characters to it. – Jerum Jan 18 '18 at 14:47
  • So if I understand correctly, you want to parse `text = "word1 word2 ... wordn"` to an array of pointers i.e. `t[0] = copy_of_word1;` ... `t[n] = copy_of_wordn;` (as regular null terminated C string), then to assign those to **newtext. So if I got this correct, did you solve your problem or wanna me to derive a function do that for you? – the kamilz Jan 19 '18 at 07:16
1

Assuming that you know the number of words, it is trivial:

char **newtext = malloc(3 * sizeof(char *));   // allocation for 3 char *
// Don't: char * pointing to non modifiable string litterals
// char * t1 = "fus", t2 = "roh", t3 = "dah";
char t1[] = "fus", t2[] = "roh", t3[] = "dah"; // create non const arrays

/* Alternatively
char text[] = "fus roh dah";    // ok non const char array
char *t1, *t2, *t3;
t1 = text;
text[3] = '\0';
t2 = text + 4;
texts[7] = '\0';
t3 = text[8];
*/
newtext[0] = t1;
newtext[1] = t2;
newtext[2] = t2;
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • Thanks, I'm trying to make this a bit more dynamic and not hardcoded by putting it in a loop. My only problem is storing the size of the last word and having the next pointer = t1 +strlen(prev word). – Jerum Jan 18 '18 at 17:04