C strings confusion

前端 未结 7 2159
深忆病人
深忆病人 2020-11-27 14:30

I\'m learning C right now and got a bit confused with character arrays - strings.

char name[15]=\"Fortran\";

No problem with this - its an

7条回答
  •  Happy的楠姐
    2020-11-27 14:55

    char *name, on it's own, can't hold any characters. This is important.

    char *name just declares that name is a pointer (that is, a variable whose value is an address) that will be used to store the address of one or more characters at some point later in the program. It does not, however, allocate any space in memory to actually hold those characters, nor does it guarantee that name even contains a valid address. In the same way, if you have a declaration like int number there is no way to know what the value of number is until you explicitly set it.

    Just like after declaring the value of an integer, you might later set its value (number = 42), after declaring a pointer to char, you might later set its value to be a valid memory address that contains a character -- or sequence of characters -- that you are interested in.

提交回复
热议问题