Quote:
Originally Posted by
MAtkins
//will the memory for idx still be allocated once it is returned?
//Does 'return' pass the allocation on or kill it even though I've returned it?
Just like function parameters, function return values are passed by value. It takes the number inside idx, puts that in a register and returns from the function call, leaving just the data and no "allocation".
Quote:
//sprintf automatically moves the 0 to the end after putting the string together??
Yes, it always null terminates, meaning you always need one more byte than you plan for that null
Quote:
//Did I successfully copy the contents of buffer to the original memory location of txt1
Nope.
Quote:
//Does ptrSt (that is, txt1) still contain the contents (in the originally allocated memory space for txt1) that was in buffer?
ptrSt was set to the first character in buffer, which should have generated a warning. You declared ptrSt as a char *, but *buffer is just a plain char. Even if you did ptrSt = buffer, you are reassigning that pointer to contain just the address of the malloc'ed memory.
Quote:
//is the memory for argv[2] & argv[3] still allocated?
Yes. txt2, txt3 and x however went out of scope when the program hit the } on line 42.
Quote:
//Does txt1 still point to the location of the original argv[1] but with a different size & a changed value?
Nope, it points to what is likely a bogus address, since you assigned ptrSt to *buffer and returned that. There's no way to resize it.
Quote:
Well I compiled it and I see that the whole thing with ptrSt doesn't work.
I just cannot figure out how to make the original memory argv[1] change in that function.
All I'm really trying to do is allocate as *little* memory as possible and get that changed value back into main.
So you would have to strcpy/strcat/sprintf directly into st in your strReplaceAll function. This is only going to work if replace is shorter than original, otherwise you would need to grow argv[1], which isn't possible in C. At least it wont happen automatically, you will need to allocating new memory for the string, but then you wont change the contents of argv[1]. You could make argv[1] itself point to the newly allocated memory however.
Quote:
How would you gurus write this stuff?
I will try to whip something up before company arrives in 20 minutes or so.