C 语言字符串

C语言中字符串末尾有一个结束符 , 占一个位置,所以在以字符数组表示字符串时,其的实际长度为声明长度 - 1,所以在使用过程中要注意数组越界。
 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 int main()
 5 {
 6      char name[8] = "itcasasa";    //error C2117: 'itcasasa' : array bounds overflow
 7      char name2[] = {'o', 'k'};
 8      printf("%s
", name);
 9 
10     return 0;
11 }  
使用 strlen 计算字符串的长度是他的实际长度,即除去 后的长度。
#include <stdio.h>
#include <string.h>

int main()
{
    char str[] = "123";
    printf("%d
", strlen(str));        //输出 3

    return 0;
}
在使用字符数组表示字符串时一定要加结束符 , 不然会得到意想不到的结果。
字符串在内存中的存储是 顺序存储的,并且是从大端开始寻址,字符串的输出是遇到结束符后停止输出。
 
 
原文地址:https://www.cnblogs.com/hyhl23/p/4163057.html