字符串的引用方式
在c语言中,字符串是存放在字符数组中的,想要引用一个字符串,有两种方法:
(1)、用字符数组存放一个字符串
通过数组名和下标引用字符串的一个字符,也可以通过数组名(即首元素地址)和格式声明“%s”输出该字符串。
#include <stdio.h>
#include <stdlib.h>
int main()
{
char string[]="I love China!";
printf("%s\n",string);
printf("%c\n",string[7]);
system("pause");
return 0;
}
(2)、用字符指针变量指向一个字符串常量
通过字符指针变量引用字符串常量。
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *string="I love China!";
printf("%s\n",string);
printf("%c\n",string[7]);
system("pause");
return 0;
}
string被定义为一个指针变量,基类型为字符型。请注意它只能指向一个字符型数据,而不能同时指向多个字符数据,更不是把"I love China!"这些字符存放在string中(指针变量只能存放地址),也不是把字符串赋给*string,只是把"I love China!"的第一个字符的地址赋给指针变量string。
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *string;
string="I love China!";
printf("%s\n",string);
printf("%c\n",string[7]);
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a[]="I love China!";
char *string=a;
printf("%s\n",string);
printf("%c\n",string[7]);
system("pause");
return 0;
}
说明:通过字符数组名或者字符指针变量可以输出一个字符串,而对一个数值型数组,是不能企图用数组名输出它的全部元素的。例如:
int a[10];
.
.
.
printf("%d\n",a);
这样是不行的,它输出的是数组首元素的地址。对于数值型数组元素的元素值只能逐个输出。
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a[]="I love China!",b[20];
int i;
for(i=0;*(a+i)!='\0';i++)
*(b+i)=*(a+i);
*(b+i)='\0';
//for循环将a串的有效数值全部赋值给了b串,最后应该将'\0'复制过去,作为字符串的结束标志
printf("string b is:");
for(i=0;b[i]!='\0';i++)
printf("%c",b[i]);
printf("\n");
system("pause");
return 0;
}
程序说明输出字符串也可以用逐个字符输出的方法。11-14行也可以换成下面这样。
printf("string b is:%s\n",b);
上面问题也可以用指针变量来处理。
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a[]="I love China!",b[20],*p1,*p2;
p1=a;p2=b;//p1 p2分别指向a和b数组的第一个元素
for(;*p1!='\0';p1++,p2++)
*p2=*p1;
*p2='\0';
printf("string b is:%s\n",b);
system("pause");
return 0;
}
下面是for循环的一般语句,表达式1表示初始化,如果前面已经初始化就可以为空,表达式2和表达式3之间一定要写分号。
for (表达式1; 表达式2; 表达式3)
{
语句;
}
当然也可以再初始化一遍
for(*p1='I';*p1!='\0';p1++,p2++)
或者
for(p1=a;*p1!='\0';p1++,p2++)
还是整个输出比较简单
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a[]="I love China!",b[20],*p1,*p2;
p1=a;p2=b;//p1 p2分别指向a和b数组的第一个元素
p2=p1;
printf("string b is:%s",p2);
system("pause");
return 0;
}
字符指针作函数参数
想把一个字符串从一个函数传递到另一个函数,可以用地址传递的方法,即用字符数组名作参数,也可以用字符指针变量作参数。在被调用的函数中可以改变字符串的内容,在主调函数中可以引用改变后的字符串。
(1)、用字符数组名作为函数参数
#include <stdio.h>
#include <stdlib.h>
int main()
{
void copy_string(char from[],char to[]);
char a[]="I am a teacher.";
char b[]="You are a student.";
printf("string a=%s\nstring b=%s\n",a,b);
printf("\n把字符串a赋给字符串b:\n");
copy_string(a,b);
printf("string a=%s\nstring b=%s\n",a,b);
system("pause");
return 0;
}
void copy_string(char from[],char to[])
{
int i=0;
while(from[i]!='\0')
{
to[i]=from[i];
i++;
}
to[i]='\0';
}
(2)、用字符型指针变量作实参
#include <stdio.h>
#include <stdlib.h>
int main()
{
void copy_string(char from[],char to[]);
char a[]="I am a teacher.";
char b[]="You are a student.";
printf("string a=%s\nstring b=%s\n",a,b);
char *from=a,*to=b;
printf("\n把字符串a赋给字符串b:\n");
copy_string(from,to);
printf("string a=%s\nstring b=%s\n",a,b);
system("pause");
return 0;
}
void copy_string(char from[],char to[])
{
int i=0;
while(from[i]!='\0')
{
to[i]=from[i];
i++;
}
to[i]='\0';
}
(3)、用字符指针变量作形参和实参
#include <stdio.h>
#include <stdlib.h>
int main()
{
void copy_string(char *from,char *to);
char *a="I am a teacher.";
char b[]="You are a student.";
char *p=b;
printf("string a=%s\nstring b=%s\n",a,b);
printf("\n把字符串a赋给字符串b:\n");
copy_string(a,p);
printf("string a=%s\nstring b=%s\n",a,b);
system("pause");
return 0;
}
void copy_string(char *from,char *to)
{
for(;*from!='\0';from++,to++)
{
*to=*from;
}
*to='\0';
}