views:

89

answers:

7

Hello,

Hopefully my title was descriptive enough to attract the right help.

I want to write a function that will return 1 thing, and modify a provided pointer in another.

My current function declaration is . . .

char * replaceURLS(char * body)

What I want to do is copy all of body's data into a new string, and set body to point to this new data. I then want afterURL to point to a location within the new string.

My issue is getting the actual pointer that is passed in to this function to point to the new data.

Thanks in advance! Rob

+1  A: 

you need: in C++

char * afterURLreplaceURLS(char *& body);

in C

char * afterURLreplaceURLS(char ** body);

then call with:

replaceURLS(&pointer)
LukeN
The tag says C, not C++. References aren't available in C. However, you can simulate the behavior of references here by creating a macro to wrap a function named something like `char *afterURLreplaceURLs_(char **body)`
Joey Adams
+1  A: 
char * afterURL replaceURLS(char ** body)
a1ex07
A: 

If you change the value of body currently, the actual pointer won't be changed, just your copy of it. Instead, you need to pass in a pointer to the pointer, as follows:

void replaceURLS(char ** body){
    *body = "The new string.";
}

Then when you use the function, do it like this:

char* randomString = "Old string"; //Containing your old string
replaceURLS(&randomString);

This should do it.

Chris Cooper
+2  A: 
char * replaceURLS(char ** body)
{
    char *newString = (char *) malloc(strlen(*body) + 1);
    strcpy(newstring, *body);

    *body = newString;

    return(newString + whateverOffsetWithinTheStringYouNeedToReturn);
}
Jason Williams
+1 for providing the most complete answer (until now). @OP: Don't forget to `free` your original string at some point. Looking at the `replaceURLS` function, this could be easy to forget because the pointer to the original string is overwritten.
stakx
Thanks a lot. Makes perfect sense.
+1  A: 

You'll need to pass in a pointer-to-pointer:

char * replaceURLS(char ** body_ptr) {
    char * newString;
    // copy data and replace URLS in newString
    *body_ptr = newString;
}

replaceURLS(&body);

(What character pointer are you returning? Presumably not newString, or you could just do body = replaceURLS(body).)

Jefromi
A: 

You are passing the address by value.

char * replaceURLS(char * body)

should be semantically replaced as actually be

char * replaceURLS(char * addressOfStartOfBody)

Because the addressOfStartOfBody is being passed by value, and not by reference, you are not able to change it. What you need to do is modify it so that it takes in the address of a pointer.

char * replaceURLS(char ** addressOfBodyAddress)

Then to call it, you would have code approximately like this

char * bodyAddress;
char * returnVal = replaceURLS(&bodyAddress);

This will pass in the address of the bodyAddress variable. Which means you will be able to change what bodyAddress points to.

A: 

This is where you need a pointer to a pointer.

Function calls are by value. When you pass a pointer, you're passing a value that represents a memory location. The function can then modify what is stored at that location in memory.

If you want to modify what a pointer is pointing at, you have has to pass a pointer to that pointer :) The code below is really not useful, but should demonstrate.

void changeMyString(char **myString)
{
  free(*myString);  
  *myString = (char*)malloc(20); /* Just as example, 20 bytes */
  sprintf(*myString,"This is new");
}    
Brian Roach