IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C语言字符数组和字符指针-学习笔记(十三) -> 正文阅读

[C++知识库]C语言字符数组和字符指针-学习笔记(十三)

一、字符串处理

1、字符串表示和处理方式

在C语言中没有专门的字符串类型,字符串可以用三种方式表示和处理:
(1)字面值方式:“xsteach”。不能修改,但是可以被赋值给变量,并在变量中修改。字面值方式的字符串后面自动追加’\0’结束符。字面值可以拼接:
" abcdg" ==> “erghjj”
(2)用字符数组:

void main( )
{
	char string[]="How are you?" ;
	printf("%s\n" , string) ;
}
//以char型数组表示字符串,结尾符'\0',必须手动加入数组中。

(3)用字符指针:
①指向字面值字符串

char *str="I love China!";//初始化 
//等价于
char *str;
str="I love china!"; //赋给str的不是字符,而是字符串第一个元素的地址。

②指向字符数组字符串。同样有效字符串必须以’\0’结尾。

char *a,str[]={"hello world!"};
a=str;

字符指针变量中存放的仅是一个地址(字符串第1个字符的地址),决不是将字符串放到字符指针变量中。

二、字符数组

1、定义

字符数组由若干个元素组成,每个元素中放一个字符。
字符数组名是常量绝不允许改变,但字符数组元素值可改变。

2、初始化

char str[14]={"I love China!"};

3、赋值

字符数组只能对其各个元素逐个赋值,不能对字符数组整体赋值。

char str[14];
strcpy(str,"I love china!");

4、字符数组地址

定义了一个字符数组,在编译时为它分配内存单元,它有确定的地址。

char str [10];
scanf ("%s",str);//输入一个字符串

5、字符数组应用

例1:

#include <stdio.h>
#include <string.h>

int main()
{
	char str[]="this is Love!";
	//str=str+7;/*非法!*/
    char a1[]="ABC";
	char a2[]={"ABC"};
    char b1[]="'A','B','C','\0'";
    char b2[]={'A','B','C','\0'};
	char b3[]={"'A','B','C','\0'"};
	
	printf("%s\n",str);//this is Love!
	str[2]='g';//修改字符串字符元素
	*(str+2)='2';
	str[6]=0;
	printf("%s\n",str);//th2s i
	str[6]='0';
	printf("%s\n",str);//th2s i0 Love!
	
	printf("a1=%d,a2=%d\n",sizeof(a1),sizeof(a2));//a1=4,a2=4
    printf("b1=%d,b2=%d,b3=%d\n",sizeof(b1),sizeof(b2),sizeof(b3));
	//b1=16,b2=4,b3=16
	return 0;
}

例2:

#include <stdio.h>
#include <string.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("字符串a=%s\n",a);
	printf("字符串b=");
	for(i=0;b[i]!='\0';i++)
	{
		printf("%c",b[i]);
	}
	printf("\n");
	return 0;
}
//执行输出:字符串a=I am a student.
//          字符串b=I am a student.

例3:

#include <stdio.h>
#include <string.h>

void copy_string(char from[],char to[])
{	
	int i=0;
	while(from[i]!='\0')
	{
		to[i]=from[i];
		i++;
	}
	to[i]='\0';
}

int main()
{
	char a[]="I am a teacher.";
	char b[]="you are a student. ";
	printf("字符串a=%s\n字符串b=%s\n",a,b);
	printf("字符串a拷贝到b:\n ");
	copy_string(a,b);
	printf("\n字符串a=%s\n字符串b=%s\n",a,b); 
	return 0;
}

例4:

#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>

int main()
{
	int  i=0,j=0;//自定义临时变量
	char src[100]="hello,world";//字符数组
	char dest[100]="i am fine";
	printf("src=%s\n",src);//输出
	printf("dest1=%s\n",dest);//输出
	while(dest[i]!='\0')
	{
		i++;
	}
	while(src[j]!='\0')
	{
		dest[i]=src[j];
		i++;
		j++;
	}
	printf("dest2=%s\n",dest);//输出
	return 0;
}

三、指向字符数组的指针变量

1、概念

(1)C语言中的字符串,若没存放到数组中,就放在常量区中,常量区中的数据不能改变。
(2)如果要修改字符串内容,就一定要把它放在数组中。
(3)新定义的指针变量,无具体的值.解决办法是,把一个已定义的字符数组的地址赋给这个指针变量。字符指针变量的值是可以改变的。

2、使用字符指针存储和访问字符串

char* pStr; //声明字符指针:
char str[10]=“hello”; //声明字符串:
pStr=str; //使用字符指针指向字符串:
//使用字符指针访问字符串,例如pStr[0]=‘a’; 该代码将第一个字符修改为’a’。

3、字符指针作函数的参数

将一个字符串从一个函数传递到另一个函数,可以用地址传递的办法,即用字符数组名作为参数或用指向字符串的指针变量作为参数。在被调用的函数中可以改变字符串的内容,在主调函数中可以得到改变后的字符串。

4、指向字符数组的指针变量应用

例1:

#include <stdio.h>
#include <string.h>

int main()
{
	int i=0,j=0;
	char buf[10]="hello";
	char *p=buf;
	
	for(i=0;i<strlen(buf);i++)
	{
		printf("buf[i]=%c\n",buf[i]);
		//printf("buf[i]=%c\n",*p++);
	}  
	return 0;
}
//输出:buf[i]=h buf[i]=e buf[i]=l buf[i]=l buf[i]=o

例2:

#include <stdio.h>
#include <string.h>

int main()
{
   char a[]="I am a student.",b[20],*p1,*p2;
   p1=a;p2=b;
   for(;*p1 !='\0';p1++,p2++)
   {
       *p2=*p1;
   }
   *p2='\0';
   printf("字符串a=%s\n",a);
   printf("字符串a=%s\n",b); 
   return 0;
}
//执行输出:字符串a=I am a student.
//          字符串a=I am a student.

例3:

#include <stdio.h>
#include <string.h>

void copy_string(char from[],char to[])
{	
	//法1
	int i=0;
	while(from[i]!='\0')
	{
		to[i]=from[i];
		i++;
	}
	to[i]='\0';
	/*//法2
	char *p1,*p2;
	p1=from;
	p2=to;
	while((*p2++=*p1++) !='\0');
	*/
}

int main()
{
	char a[]="I am a teacher.";
	char b[]="you are a student.";
	char *from=a,*to=b;   
	printf("字符串a=%s\n字符串b=%s\n",a,b);
	printf("字符串a拷贝到b:\n ");
	copy_string(from,to);
	printf("\n字符串a=%s\n字符串b=%s\n",a,b); 
	return 0;
}

例4:

#include <stdio.h>
#include <stdlib.h>//malloc的头文件
#include <string.h>

void copy_string (char *from,char *to)
{
	//法1
	for(;*from !='\0';from++,to++)
	{
		*to=*from;
	}
	*to ='\0';
	/*//法2
	while((*to=*from) !='\0')
	{
    to++;
	from++;
	}
	*/
	/*//法3
	while((*to++=*from++) !='\0');
	*/
	/*//法4
	while(*from !='\0')
	{
    *to++=*from++;
	}
	*to ='\0';
	*/
	/*//法5
	while(*from)
	{
    *to++=*from++;
	}
	*to ='\0';
	*/
	/*//法7
	while(*to++=*from++);
	*/
	/*
	for(;(*to++=*from++)!='\0';);
	*/
	/*//法8
	for(;*to++=*from++;);
	*/
	/*//法9
	char *p1,*p2;
	p1=from;
	p2=to;
	while((*p2++=*p1++) !='\0');
	*/
}

int main()
{
	char *a="I am a teacher.";
	char b[]="you are a student. ";
	char *p=b;
    
	printf("字符串a=%s\n字符串b=%s\n",a,b);
	printf("字符串a拷贝到b:\n ");
	copy_string(a,p);
	printf("\n字符串a=%s\n字符串b=%s\n",a,b);  
	return 0;
}

例5:

#include <stdio.h>
#include <string.h>

int main()
{
	char acbuf[]={"hello"};
	char *p;
	int i=0,len=0;
	p=acbuf;
	len=strlen(acbuf);
	for(i=len-1;i<=len && i>=0;i--)//逆序输出
	{
		printf("%c",*(p+i));
	}
	printf("\n");
	return 0;
}

四、指向字符串常量的字符指针

1、概念

(1)可以声明字符指针直接指向字符串常量,例如char* pStr=“Welcome”;字符指针pStr将指向字符串常量“Welcome”,“Welcome”为字符串常量,因此其值不能修改,例如代码pStr[0]=‘h’;将引发一个错误。
(2)C语言对字符串常量按字符数组处理,在内存中开辟了一个字符数组用来存放该字符串变量,但这个字符数组没有名字,不能通过数组名引用,只能通过指针变量来引用。对字符指针变量初始化,实际指向字符串第一个元素的地址。
(3) C语言中的字符串,如果没存放到数组中,就是放在常量区中,而常量区中的数据是不能改变的。换言之如果要修改字符串内容,就一定要把它放在数组中。

2、指向字符串常量的字符指针应用

例1:

#include "stdio.h"
#include "string.h"

void main( )
{   
	int i=0;
	char *p="china";
	char *pt="ABCDEFGHIJK";
	printf("输出单个字符:");
	printf("p[2]=%c\n",p[2]);//p[2]=i
	printf("整串输出:");
	printf("%s\n",p);//china
	printf("指针法逐个字符输出:");
	while (*p!='\0')
	{
		printf("%c",*p++);
	}
	printf("\n下标法输出字符串:");//china
	for(i;pt[i]!='\0';i++)
	{
		printf("%c",pt[i]);
	}
    printf("\npt[3]=");//ABCDEFGHIJK
	putchar(pt[3]);//D
	printf("\n");
	//pt[2]='x';//非法赋值
	printf("pt=%s\n",pt);//ABCDEFGHIJK
	pt=pt+2;
	printf("pt+2=%s\n",pt);//pt+2=CDEFGHIJK
    return;
}
//注:指针变量的值可以改变,但指针变量所指向的元素值不能改变。

例2:

#include <stdio.h>
#include <stdlib.h>//malloc的头文件
#include <string.h>

void Mycopy(char *des,char *src)//字符串复制函数
{
	while(*src!='\0')//字符串最后的字符为\0
	{
        //*des++=*src++;//两种方式都可以
        *des=*src;
        des++;
        src++;  
	}
}

int main(int argc,char const *argv[])
{
	char *str1="this is a test";
	char *str2=(char *)malloc(strlen(str1));//开辟空间
	Mycopy(str2,str1);//调用字符串复制函数
	printf("str2=%s\n",str2);//输出复制过后的字符串
	return 0;
}

例3:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void myStrCpy(char **des, char *src)
{
	int i = 0;
	if(*des == NULL)
	{
		printf("malloc\n");
		*des = (char *)malloc(strlen(src)*sizeof(char));
	}
	while(*src != '\0')
	{
		**des = *src;
		(*des)++;
		src++;
		i++;
	}
	*des=*des-i;
}

int main(int argc, char const *argv[])
{
	char *str1 = "this is a test";
	char *str2 = NULL;
	printf("address of \"this is a test\" is %p\n", str1);
	printf("address of str2 is %p\n", &str2);
	
	myStrCpy(&str2,str1);
	printf("str2: %s\n", str2);

	return 0;
}
//输出:
//address of "this is a test" is 00422FB4
//address of str2 is 0019FF28
//malloc
//str2: this is a test葺葺葺葺葺葺葺倭娑

例4:

#include <stdio.h>
#include <stdlib.h>//malloc的头文件
#include <string.h>

int Mycopy(char *des,char *src,int m)//字符串复制函数
{
	while(*src!='\0' && m>0)//判断输入字符开始位置是否正确
	{
        src++; 
        m--; 
	}
	if(m==0)
	{
        while(*src!='\0')
		{
			*des++=*src++;
		}
	}
	else
	{
        printf("你要开始拷贝的位置,超过了源字符串\n");
        return -1;
	}
	return 0;
}

int main(int argc,char const *argv[])
{
	char *str1="this is a test";
	char *str2;
	int ret;
	str2=(char*)malloc(sizeof(char)*strlen(str1));//开辟空间
	
	ret=Mycopy(str2,str1,13);//调用字符串复制函数
	if(ret==-1)
	{
		printf("拷贝出错,退出\n");
		exit(-1);
	}
	printf("str1:%s\n",str1);
	printf("str2:%s\n",str2);
	return 0;
}
//输出:
//str1:this is a test
//str2:t屯屯屯屯屯屯妄葺葺葺葺葺葺輻釸.殨

例5:

#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>

void mycopy(char *dest,char *src,int len)
{
	int  i=0;//自定义临时变量
	char *s=src;
	while(*s!='\0')
	{
		*s++;
		i++;
		if(i==10)
		{
			printf("*s1=%c\n",*s);
		}
	}
	printf("i=%d\n",i);
	printf("len=%d\n",len);
	while(len--!=0)//字符串长度不为0 --
	{  
		*dest++=*(--s); //法1
		/*法2
		s--;// \0代表中止,指针先减1
		printf("before copy s=%c\n",*s);
		*dest=*s;
		printf("after copy dest=%c\n",*dest);
		dest++;*/		
	}
}

int main()
{
	char *src="hello,world";//字符指针赋值
	char *dest=(char *)malloc(strlen(src));//字符指针初始化为NULL
	int  len=strlen(src);//计算字符串个
	
	mycopy(dest,src,len);//字符串逆序拷贝
	printf("dest2=%s\n",dest);//输出逆序字符串
	return 0;
}
//程序运行结果:
//*s1=d
//i=11
//len=11
//dest2=dlrow,olleh軶-薨?
//Press any key to continue

例6:

#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
void connect(char *dest,char *string,int len)
{
	int  i=0,j=0;//自定义临时变量
	while(dest[i]!='\0')
	{
		i++;
	}
	printf("i=%d\n",i);
	while(string[j] !='\0')//数组字符串长度
	{        
        dest[i]=string[j];
        i++;
		j++;
	}
    dest[i]='\0';
}

int main()
{	
	char src[20]="hello,world";//字符指针赋值
	char *str="i am fine";
	int  len=sizeof(str);//计算数组大小
	printf("src1=%s\n",src);
	connect(src,str,len);
	printf("src2=%s\n",src);
	return 0;
}

例7:

#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>

void connect(char *dest,char *string,int len)
{
	int  i=0,j=0;//自定义临时变量
	while(*dest!='\0')
	{
		*dest++;
		i++;
	}	
	printf("len=%d\n",len);
	dest--;
	while(len--!=0)//数组字符串长度--while(*string !='\0')//数组字符串长度
	{        
        *dest=*string;
        dest++;
		 string++;
	}
    *dest='\0';
}

int main()
{	
	char src[100]="hello,world";//字符指针赋值
	char *str="i am fine";
	int  len=strlen(str);//计算字符串个数
	printf("src1=%s\n",src);
    connect(src,str,len);
    printf("src2=%s\n",src);
	return 0;
}

编辑 2020-09-13 02:48 首次编辑
增改 2021-07-15 23:26 内容结构优化

注:本文旨于作为自己的学习笔记,不作他用。

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-07-16 11:05:17  更:2021-07-16 11:05:38 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/5 18:46:01-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码