C风格字符串
如下示例代码,c文件,
// // sub1.c // HelloWorld // // Created by Yale Li on 15/10/3. // Copyright © 2015年 Yale Li. All rights reserved. // #include <stdio.h> #include <stdlib.h> #include <string.h> void travel_str(){ char x = 0x21; //字符 ! while (x<=0x7e) { printf("%x is %c\n",x,x); x++; } } void test_c_style_str(){ //创建五个字符大小的内存空间,并返回首地址 char *start = malloc(5); *start = 'L'; *(start + 1) = 'o'; *(start + 2) = 'v'; *(start + 3) = 'e'; *(start + 4) = '\0'; // 指针的下标运算 // start[0] = 'L'; // start[1] = 'o'; // start[2] = 'v'; // start[3] = 'e'; // start[4] = '\0'; printf("%s has %zu characters\n",start,strlen(start)); printf("the third letter is %c\n",*(start + 2)); free(start); start = NULL; return; } //分配连续内存地址空间 void test_malloc(){ //分配3个float大小的内存地址空间,并返回首地址 float *start = malloc(3 * sizeof(float)); start[0] = 1.2; start[1] = 1.3; start[2] = 1.4; for (int i = 0; i<3; i++) { printf("the value is %f\n",start[i]); } free(start); start = NULL; } //字符串的字面量 void test_string_literal(){ const char *start = "Love"; //直接定义字符串字面量 while (*start!='\0') { printf("%x is %c\n",*start,*start); start++; } } //定义字符数组 //在程序中往往依靠检测'\0'的位置来判定字符串是否结束,而不是根据数组的长度来决定字符串长度 void test_char_array(){ //指定字符数组的长度是10,但是只初始化了其中的前9个数组元素,最后一个自动初始化为空字符,即'\0' //字符数组的长度为10,但是字符串的长度为9,结束符不算进字符串的长度 const char str1[10] = {'c',' ','p','r','o','g','r','a','m'}; printf("the 10th letter is %x\n",str1[9]); const char *p1 = str1; while (*p1!='\0') { printf("%c",*p1); p1++; } printf("\n"); //显示指定了结束符,算上结束符,数组的长度为10,但是字符串的长度为9,字符串的长度不包括结束符 const char str2[10] = {'c',' ','p','r','o','g','r','a','m','\0'}; const char *p2 = str2; while (*p2!='\0') { printf("%c",*p2); p2++; } printf("\n"); //其余的自动初始化为空字符,即'\0',也可以显示的指定字符串的结束符 char str3[10] = {'h','a','p','p','y'}; char *p3 = str3; while (*p3!='\0') { printf("%c",*p3); p3++; } printf("\n"); char str4[5] = {'h','a','p','p','y'}; //数组长度为5,没有结束符 printf("the str4 value is %s\n",str4); char str5[6] = {'h','a','p','p','y','\0'};//包含结束符在内的数组长度为6 //初始化字符数组时,不指定数组的长度 //同时没有指定字符串结束符 //这里数组的长度为7,而字符串的长度由于没有字符串结束符而不确定 char str6[] = {'c','h','i','n','e','s','e'}; for (int i = 0; i<strlen(str6); i++) { printf("%c",str6[i]); } printf("\n"); //初始化字符数组时,不指定数组的长度 //指定字符串结束符 //这里数组的长度为8,同时字符串的长度为7 char str7[] = {'c','h','i','n','e','s','e','\0'}; for (int i = 0; i<strlen(str7); i++) { printf("%c",str7[i]); } printf("\n"); //用字符串常量来初始化字符数组 //编译器会自动加上字符串结束符 char str8[] = {"welcome1"}; for (int i = 0; i<strlen(str8); i++) { printf("%c",str8[i]); } printf("\n"); //用字符串常量来初始化字符数组,可以去掉花括号 //这种方式初始化字符数组会自动加上字符结束符 char str9[] = "welcome2"; for (int i = 0; i<strlen(str9); i++) { printf("%c",str9[i]); } printf("\n"); //用字符串常量来初始化字符数组,可以去掉花括号 //使用字符串常量初始化字符数组时,指定了长度,该长度不包含字符串结束符 //数组的长度是一定的,而字符串的长度不确定 char str10[8] = "welcome3"; for (int i = 0; i<strlen(str10); i++) { printf("%c",str10[i]); } printf("\n"); printf("the length of str1 is %ld\n",strlen(str1)); printf("the length of str2 is %ld\n",strlen(str2)); printf("the length of str3 is %ld\n",strlen(str3)); printf("the length of str4 is %ld\n",strlen(str4)); printf("the length of str5 is %ld\n",strlen(str5)); printf("the length of str6 is %ld\n",strlen(str6)); printf("the length of str7 is %ld\n",strlen(str7)); printf("the length of str8 is %ld\n",strlen(str8)); printf("the length of str9 is %ld\n",strlen(str9)); printf("the length of str10 is %ld\n",strlen(str10)); printf("\n"); } //字符串数组 void test_char_pointer_array(){ //定义一个字符串数组 char *strs[5] = { "CHINA","AMERICA","AUSTRALIA","FRANCE","GERMAN"}; //遍历字符串数组 for (int i=0;i<5;i++){ printf("%s\n",strs[i]); } }
main文件,
#include <stdio.h> extern void travel_str(); extern void test_c_style_str(); extern void test_malloc(); extern void test_string_literal(); extern void test_char_array(); extern void test_char_pointer_array(); int main(int argc, const char * argv[]){ travel_str(); test_c_style_str(); test_malloc(); test_string_literal(); test_char_array(); test_char_pointer_array(); return 0; }
输出打印,只贴出字符串长度的打印,
the length of str1 is 9
the length of str2 is 9
the length of str3 is 5
the length of str4 is 11
the length of str5 is 5
the length of str6 is 12
the length of str7 is 7
the length of str8 is 8
the length of str9 is 8
the length of str10 is 16
=====END=====