0

I am attempting to change the value of an original string by changing a pointer.

Say I have:

char **stringO = (char**) malloc (sizeof(char*));
*stringO = (char*) malloc (17);    
char stringOne[17] = "a" ;
char stringTwo[17] = "b";
char stringThree[17] = "c";
char newStr[17] = "d";
strcpy(*stringO, stringOne);
strcpy(*stringO, stringTwo);
strcpy(*stringO, stringThree);
//change stringOne to newStr using stringO??

How can I change stringOneso its the same as newStr using the pointer stringO?

edit: I guess the question was rather unclear. i want it to modify the latest string that *strcpy was copied from. So if strcpy(*stringO, stringThree); was last called, it will modify stringThree, strcpy(*stringO, stringTwo); then string Two etc.

gta0004
  • 508
  • 4
  • 11
  • 29
  • 1
    You shouldn't cast the result of `malloc`. – chris Aug 16 '13 at 05:00
  • 1
    @chris ([explanation](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858#605858)) –  Aug 16 '13 at 05:14
  • @H2CO3 the other answers on that thread point to good reasons to include the cast – gta0004 Aug 16 '13 at 05:19
  • @gta0004 There are reasons for including the cast. There may even be some good reasons. But they can't be good enough. If you are writing C code, you must not cast the return value, as explained by unwind in the answer I linked to. Basically, nothing but "you can compile your code as C++" *could be* the only valid reason for the typecast - but you know, no sane programmer tries to compile C code as C++ because they are different and very distinct languages. –  Aug 16 '13 at 05:42

2 Answers2

2

I want it to modify the latest string that strcpy was copied from. So if strcpy( ( *stringO ), stringThree ); was last called, it will modify stringThree, strcpy( (*stringO ), stringTwo ); then stringTwo etc.

It is not possible to do this with your approach since you are making a copy of the string by using strcpy -- not pointing to blocks of memory. To achieve your goal I would do the following:

char *stringO = NULL;

char stringOne[ 17 ] = "a";
char stringTwo[ 17 ] = "b";
char stringThree[ 17 ] = "c";
char newStr[ 17 ] = "d";

stringO = stringOne; // Points to the block of memory where stringOne is stored.
stringO = stringTwo; // Points to the block of memory where stringTwo is stored.
stringO = stringThree; // Points to the block of memory where stringThree is stored.

strcpy( stringO, newStr ); // Mutates stringOne to be the same string as newStr.

... note that I am mutating (updating) where stringO points to, not copying a string into it. This will allow you to mutate the values in the blocks of memory that stringO points as (which is consequently where the latest stringXXX is stored) -- as requested.

Jacob Pollack
  • 3,703
  • 1
  • 17
  • 39
1

Here's one way:

char **stringO = (char**) malloc (sizeof(char*));
char stringOne[17] = "a" ;
char stringTwo[17] = "b";
char stringThree[17] = "c";
char newStr[17] = "d";

*stringO = stringOne;
strcpy(*stringO, newStr);

If I have to use stringO the way you have allocated memory for it, then:

strcpy(*stringO, newStr);
strcpy(stringOne, *stringO);
jxh
  • 69,070
  • 8
  • 110
  • 193
  • @gta0004: I don't understand. What didn't work for you? See [this](http://ideone.com/vDdnQz) and [this](http://ideone.com/kBtw33). – jxh Aug 16 '13 at 06:13