Passing char pointer in C

后端 未结 5 893
时光说笑
时光说笑 2020-12-08 05:53

Okay so I am trying to pass a char pointer to another function. I can do this with an array of a char but cannot do with a char pointer. Problem is I don\'t know the size of

5条回答
  •  借酒劲吻你
    2020-12-08 06:33

    void ptrch ( char * point) {
        point = "asd";
    }
    

    Your pointer is passed by value, and this code copies, then overwrites the copy. So the original pointer is untouched.

    P.S. Point to be noted that when you do point = "blah" you are creating a string literal, and any attempt to modify is Undefined behaviour, so it should really be const char *

    To Fix - pass a pointer to a pointer as @Hassan TM does, or return the pointer as below.

    const char *ptrch () {
        return "asd";
    }
    
    ...
    const char* point = ptrch();
    

提交回复
热议问题