In C, are arrays pointers or used as pointers?

前端 未结 6 894
野趣味
野趣味 2020-11-22 05:23

My understanding was that arrays were simply constant pointers to a sequence of values, and when you declared an array in C, you were declaring a pointer and allocating spac

6条回答
  •  囚心锁ツ
    2020-11-22 05:43

    In

    char hello[] = "hello there"
    int i;
    

    and

    char* hello = "hello there";
    int i;
    

    In the first instance (discounting alignment) 12 bytes will be stored for hello with the allocated space initialised to hello there while in the second hello there is stored elsewhere (possibly static space) and hello is initialised to point to the given string.

    hello[2] as well as *(hello + 2) will return 'e' in both instances however.

提交回复
热议问题