0

In C++, I can do this:

    #include <stdio.h>
    void ChangeAddress(char *&para)
    {
         char *temp = "123456";
         para = temp;
    }

    int main()
    {
    char *para = "abcdef";
    ChangeAddress(para);
    printf("%s\n",para);//123456
    return 0;
    }

So is there any alternative way in C?

1
  • You are not changing the address of the parameter there. Commented Dec 15, 2011 at 14:14

1 Answer 1

10

Replace reference with pointer:

#include <stdio.h>
void ChangeAddress(char ** para)
{
     char *temp = "123456";
     *para = temp;
}

int main()
{
char *para = "abcdef";
ChangeAddress(&para);
printf("%s\n",para);//123456
return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.