我们可以通过下面方式来初始化一个字符串
char arr[] = "this is array"; char * pointer = "this is pointer";输出如下
this i this i输出如下
this i this i指针用增量运算符,数组用for循环输出每个char
while(*(pointer) != '\0') putchar(*(pointer++));输出如下
this is pointer数组修改数据方法如下
arr[5] = 'z'; printf("this is array"); printf(": chang to %ss \n","this is array"); printf("%s",arr);输出如下
this is array: chang to this is array this zs array指针修改的话,会存在问题,可能有些编译器会直接编译不通过
pointer[5] = 'z'; printf("this is pointer"); printf(": chang to %ss \n","this is pointer"); printf("%s",pointer);如果编译通过,可能会有如下输出
this zs pointer: chang to this zs pointer this zs pointer这原因可能为,编译器用相同地址来替代this is pointer。所以,我们printf("this is pointer");中的字符串的指向地址也是pointer,所以我们对pointer的修改也会影响到最后的结果。
输出如下,指针数组的3对应arrs[3][20]中的3
this is arr2 this is pointer2源码地址:https://github.com/oDevilo/C