C sizeof char pointer

后端 未结 5 1004
感动是毒
感动是毒 2021-01-07 08:41

Why is size of this char variable equal 1?

int main(){

char s1[] = \"hello\";

fprintf(stderr, \"(*s1) : %i\\n\", sizeof(*s1) )    // prints out 1

}
         


        
5条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-07 09:47

    Why is size of this char variable equal 1?

    Because size of a char is guaranteed to be 1 byte by the C standard.

    *s1 == *(s1+0) == s1[0] == char
    

    If you want to get size of a character pointer, you need to pass a character pointer to sizeof:

    sizeof(&s1[0]);
    

提交回复
热议问题