C语言系列-字符串,数组,指针

今天就结束指针系列,因为毕竟不能太深入,这样容易在脑子里成为浆糊。所以,重点放在字符串,从字符串角度更好的理解数组和指针。还是英文加中文解释。英文这次选择双语版C程序设计的有关部分。

A string literal(or string) is any sequence of characters enclosed in double quotation marks. i.e. "Hello" and "I love c" are string literals. We have used strings in printf() to display a message on the screen as in printf("Input a number");

The characters of a string are stored in contiguous memory location. A strig is terminated by the null character '\0'. The null character is a single byte of memory containing a zero.

如果你学过其他程序语言,很可能对字符串很熟悉,但C语言里的字符串却是另一回事,所以,不要相互间搞混。C语言的字符串,其实就是一个数组,一个字符类型的数组,只不过在结尾有一个空字符‘\0’作为字符串的结束符。当然,字符串可以看做是双引号包括起来的字符系列。

In C, a string is an array with elements of type char. The number of elements in the array is eaqual to the number of characters in the string plus one for the terminating null character '\0'.

这里强调了,一个字符串就是 元素为char 类型的数组,数组中的字符数量加上一个0结尾的字符串结束符,所以数组元素个数比字符多一个。如果没有这个结尾的的东西,那么那就仅仅是一个字符数组,而不是字符串了。

In C, a string literal is an array of characters, and the name of the array is a pointer to its first element. Therefore, C treats a string literal as a pointer to the first character in the string.

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20190207G0LYU100?refer=cp_1026
  • 腾讯「云+社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。

扫码关注云+社区

领取腾讯云代金券