problem with chars
1 2 3 4 5 6 7 8 9 10 11 12
|
string words[...];
...
char * args[10];
for(int i = 0; i < numOfWords; i++){
const int wordLen = words[i].length();
char tempChar [wordLen];
for(int k = 0; k < wordLen; k++){
tempChar[k] = words[i].at(k);
}
args[i] = tempChar;
}
| |
args only gets the last result, or tempChar.
is there a way to assign all the different words into each args?
thanks.
Last edited on
Have you allocated memory for each of the arg arrays? It looks like all the args in the code above point to tempchar so they will all be the same.
Try this (I haven't tried compiling it).
1 2 3 4 5 6 7 8 9 10
|
#include <string.h> // include for strdup()
string words[...];
. . .
char * args[10];
for (int i = 0; i < numOfwords && i < 10; i++)
{
args[i] = strdup(words[i].c_str());
}
| |
Note strdup is like malloc so each arg needs to be free'd
thanks a million, man. that helped!
yeah, i figured that they are pointing at the same location... just din know what to do to take care of that.
Topic archived. No new replies allowed.