/* 创建时间:20171231
创建人:fangweijun(773714759@qq.com)
功能:定义一个字符数组,在其中存放字符串“i love china!”,输出该字符串和第8个字符(指针)
位置:C程序设计(第四版)2010年6月第四版 255页例8.16
*/
#include<stdio.h>
int main()
{
char string[]="I love China!";
printf("%s\n",string);
printf("%c\n",string[7]);
return 0;
}
/*Dev-c++输出结果:
I love China!
C
*/
/* 创建时间:20171231
创建人:fangweijun(773714759@qq.com)
功能:通过字符指针变量输出一个字符串(指针)
位置:C程序设计(第四版)2010年6月第四版 256页例8.17
*/
#include<stdio.h>
int main()
{
char * string="I love China!";
printf("%s\n",string);
return 0;
}
/*Dev-c++输出结果:
I love China!
*/
/* 创建时间:20171231
创建人:fangweijun(773714759@qq.com)
功能:将字符串a复制为字符串b,然后输出字符串b(指针)
位置:C程序设计(第四版)2010年6月第四版 258页例8.18
*/
#include<stdio.h>
int main()
{
char a[]="I am a student.",b[20];
int i;
for(i=0;* (a+i)!='\0';i++)
* (b+i)=* (a+i);
* (b+i)='\0';
printf("string a is:%s\n",a);
//用下标法输出:
printf("string b is:");
for(i=0;b[i]!=0;i++)
printf("%c",b[i]);
printf("\n");
return 0;
}
/*Dev-c++输出结果:
string a is:I am a student.
string b is:I am a student.
*/
/* 创建时间:20171231
创建人:fangweijun(773714759@qq.com)
功能:将字符串a复制为字符串b,然后输出字符串b,用指针变量输出(指针)
位置:C程序设计(第四版)2010年6月第四版 258页例8.19
*/
#include<stdio.h>
int main()
{
char a[]="I am a boy.",b[20],* p1,* p2;
p1=a;
p2=b;
for(;* p1!='\0';p1++,p2++)
* p2=* p1;
* p2='\0';
printf("string a is:%s\n",a);
printf("string b is:%s\n",b);
return 0;
}
/*Dev-c++输出结果:
string a is:I am a boy.
string b is:I am a boy.
*/
/* 创建时间:20171231
创建人:fangweijun(773714759@qq.com)
功能:用函数调用实现字符串的复制(指针)
位置:C程序设计(第四版)2010年6月第四版 259页例8.20
*/
#include<stdio.h>
//int main()
//{
// void copy_string(char from[],char to[]);
// char a[]="I am a teacher.";
// char b[]="You are a student.";
// char * from=a,* to=b;
// printf("string a=%s\nstring b=%s\n",a,b);
// printf("copy string a to string b:\n");
copy_string(a,b);//用字符数组名做实参
// copy_string(from,to);//用字符指针变量做实参
// printf("\nstring a=%s\nstring b=%s\n",a,b);
// return 0;
//}
//void copy_string(char from[],char to[])//形参为字符数组
//{
// int i=0;
// while(from[i]!=0)
// {
// to[i]=from[i];
// i++;
// }
// to[i]='\0';
//}
//用字符指针变量作形参和实参
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("copy string a to string b:\n");
copy_string(a,p);//用字符指针变量做实参
printf("\nstring a=%s\nstring b=%s\n",a,b);
return 0;
}
//void copy_string(char * from,char * to)//形参为字符指针变量
//{
for(;* from!='\0';from++,to++)
* to=* from;
* to='\0';
//(1)精炼写法:
while((* to=* from)!='\0')
{
to++;
from++;
}
//
(2)更加精炼写法:
while((* to++ = * from++)!='\0');
//
(3)还可以写成:
while(* from!='\0')
* to++=* from++;
* to='\0';
//
(4)ASCII码中'\0'是0,所以可以用* from代替* from!='\0'
while(* from)
* to++=* from++;
* to='\0';
//
(5)上面的while语句可以简化为(和(2)等价):
while(* to++=* from++);
* to='\0';
//
(6)函数体也可以改为只用一个for语句:
for(;(* to++=* from++)!=0;);
或
for(;* to++=* from++;);
//}
//也可以用字符数组名做函数形参,在函数中另定义两个指针变量p1,p2。含糊copy_string可写为:
void copy_string(char from[],char to[])
{
char * p1,* p2;
p1=from;
p2=to;
while((* p2++=* p1++)!='\0');
}
/*Dev-c++输出结果:
string a=I am a teacher.
string b=You are a student.
copy string a to string b:
string a=I am a teacher.
string b=I am a teacher.
*/
/*Dev-c++输出结果:
string a=I am a teacher.
string b=You are a student.
copy string a to string b:
string a=I am a teacher.
string b=I am a teacher.
*/
/* 创建时间:20171231
创建人:fangweijun(773714759@qq.com)
功能:改变指针变量的值(指针)
位置:C程序设计(第四版)2010年6月第四版 265页例8.21
*/
#include<stdio.h>
int main()
{
C程序设计 第四版(谭浩强)-学习笔记-第十一天
最新推荐文章于 2023-05-07 16:36:06 发布