sizeof on argument

前端 未结 4 1884
感动是毒
感动是毒 2021-01-29 09:40

Even with int foo(char str[]); which will take in an array initialized to a string literal sizeof doesn\'t work. I was asked to do something like strlen and the app

4条回答
  •  自闭症患者
    2021-01-29 09:55

    C strings are just arrays of char. Arrays are not passed by value in C; instead, a pointer to their first element is passed.

    So these two are the same:

    void foo(char blah[]) { ... }
    void foo(char *blah)  { ... }
    

    and these two are the same:

    char str[] = "Hello";
    foo(str);
    
    char *p = str;
    foo(p);
    

提交回复
热议问题