user1100006
user1100006

Reputation: 13

How to change address of parameter within a C function?

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?

Upvotes: 0

Views: 305

Answers (1)

arrowd
arrowd

Reputation: 34391

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;
}

Upvotes: 10

Related Questions