cplusplus.com cplusplus.com
cplusplus.com   C++ : Forums : UNIX/Linux Programming : problem with chars
  Search:
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forums
Forums
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programm...
Articles
Lounge
Jobs

-

post  problem with chars

theChameleon (12)
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
bnbertha (404)
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
|
theChameleon (12)
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.
|

This topic is archived - New replies not allowed.
Home page | Privacy policy
© cplusplus.com, 2000-2009 - All rights reserved - v2.2
Spotted an error? contact us