How to change the actual argument passed to a function in C?

前端 未结 4 1825
没有蜡笔的小新
没有蜡笔的小新 2020-12-22 13:15

I want to change the actual argument passed to a function and not a copy of it. For example:

char str[] = \"This is a string\";

I want to c

4条回答
  •  悲&欢浪女
    2020-12-22 13:41

    I think you mean something like this:

    void update_string(char ** ptr)
    {
        *ptr = strdup("This is a test");
        return;
    }
    

    Then call the function like this:

    char * str = strdup("hello world\n");
    printf("%s\n", str);
    update_string(&str);
    printf("%s\n", str);
    

提交回复
热议问题