C sizeof char pointer

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

    NOTA: the original question has changed a little bit at first it was: why is the size of this char pointer 1

    sizeof(*s1)

    is the same as

    sizeof(s1[0]) which is the size of a char object and not the size of a char pointer.

    The size of an object of type char is always 1 in C.

    To get the size of the char pointer use this expression: sizeof (&s1[0])

提交回复
热议问题