char *str='This is a string.';
初始化字符指针。 在这种情况下,字符指针指向字符串常量的起始地址,即字符串的起始地址。
这里请注意字符指针和字符数组的区别。 例如,有说明文。
char string[ ]='This is a string.';
此时,string是字符数组,其中包含字符串。
字符指针str与字符数组string的区别在于,str是一个变量,可以更改str以指向不同的字符串,但不能更改str指向的字符串常量。 string是数组,可以更改数组中存储的内容。
如果有:
char *str,*str1='This is another string.';
char string[100]='This is a string.';
程序可以使用以下语句:
str; /*指针str上显示1 */
str='This is a NEW string.'; /*指针指向新字符串常量*/
str=str1; /*改变指针str的方向*
strcpy(string,' This is a NEW string.' ) (改变字符串的内容) /
strcat(string,str )/*进行串行连接操作(/
在程序中不能执行以下操作:
串; /*无法运算数组名称*/
string='This is a NEW string.'; /*错误的字符串操作*
字符串=str 1; /*无法为数组名称赋值*/
strcat(str,' This is a NEW string.' )不能串行连接在str的后面(/
无法交叉复制到strcpy(str,string )/*str
使用中应特别注意字符指针和字符数组之间的差异。