C sizeof char pointer

后端 未结 5 1002
感动是毒
感动是毒 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:23

    sizeof(*s1) means "the size of the element pointed to by s1". Now s1 is an array of chars, and when treated as a pointer (it "decays into" a pointer), dereferencing it results in a value of type char.

    And, sizeof(char) is always one. The C standard requires it to be so.

    If you want the size of the whole array, use sizeof(s1) instead.

提交回复
热议问题