字符和字符串(字符数组)处理

几个常见特殊字符的整型数字 
描述                                              char                                                     int 
空格                                                ' '                                                         32 
 TAB键                                           '/t'                                                          9
回车换行                                        LF                                                       10
文件结束                                        EOF                                                   -1
字符串结束符号                            '/0'                                                        40
   

  TAB字符处理要小心,经过到记事本copy/paste后,TAB键被转化成几个空格   
for(;str[i]==' '||str[i]=='        ';i++);
 
但经过到记事本copy/paste后,TAB键被转化成几个空格
所以系统总报warning:
tmp.c:58: warning: comparison is always false due to limited range of data type
tmp.c:59:27: warning: character constant too long for its type
 


    ‘a’与” a”的区别(实际就是字符和字符串的区别)
‘a’
  1字节  ‘a’
” a”
  2字节
  ‘a’’/0’

       
字符串数组char str[20],至少有一个是0
strcpy(str,”123”);
 
str[0]=’1’
str[1]=’2’
str[2]=’3’
str[3]=0
 


    字符串指针=字符串数组名=字符串数组第一个元素的地址(&)
    char *s;
    char str[20];
    s =  str  = &str[0]

字符串数组和字符串指针的相同和不同

作为函数参数 ,字符串数组名和字符串指针没区别 
    showtable(char s[50]);
    showtable(char *s);

在printf上 ,字符串数组和字符串指针没区别
char s[20];
char *p ;
p="a point";
sprintf(s,"a char arrage");
printf("p's value is : %s /n",p );
printf("s's value is : %s /n",s );
 
 
p's value is : a point
s's value is : a char arrage
 
字符串数组和字符串指针在赋值上不同
字符串数组无法直接赋值,只能用sprintf,strcpy,strcat    char s[20];
char *p;
char s[20];

p="a point";

sprintf(s,"a char arrage");
strcpy(s,"a char arrage");
 


  字符串指针只支持printf,不支持scanf,scanf还是建议用字符串数组 
   对指针初始赋值,不分配空间,可以支持printf
char *p;
p="ppp";
printf("p's value is : %s/n",p);

[macg@localhost mysqltmp]$ ./tt
p's value is : ppp
 
   对指针初始赋值,不分配空间,不支持scanf
char *p;
p="ppp";
scanf("%s",p);
 
[macg@localhost mysqltmp]$ ./tt
Segmentation fault
 


    如果使字符串指针支持scanf操作,必须用malloc分配字节
#include <stdlib.h>
main()
{
char s[20];
char *p;
int ret;
p=malloc(20);
scanf("%s",p);
printf("p's value is : %s/n",p);
free(p);
 
[macg@localhost mysqltmp]$ ./tt
dddd
p's value is : dddd
 


   对malloc要求:
    #include<stdlib.h>
    malloc返回指针   p=malloc(20);
    记着要free(指针)  free(p);


    虽然也可以用字符串数组初始化字符串指针,但不建议
             实际就等于操作的是字符串数组了,多此一举
char s[20];
char *p;
p=s;   


    字符串不建议直接赋值和比较,用函数strcpy
strcpy(char*,char *)
 字符串拷贝函数
后者拷贝到前者
 
strcat(char*,char *)
 字符串追加函数
后者追加到前者后
 
strcmp(char*,char *)
 对字符串是不允许做==或!=的运算
只能用字符串比较函数
 
 
 
 字符串比较只能采用strcmp


    不能用同一个字符串数组赋值给多个字符串指针,这样会造成两个字符串指向同一空间 .
char *name,*media,a[30],s[80],c;
name=media=a;
以后修改meida,name也会跟着改变
 

 
      
    gets读字符串也有”连读”问题, 不过这个影响的不是scanf前面剩下的'/n',而是前面剩下的0
Char a[20];
gets(a)
while (a[0]==0) gets(a);   


    scanf读字符串,只能读到空格,所以scanf只能读单个单词的字符串,不能读"句子"
char s[80];

scanf("%s",s);
printf("your input is :%s",s);
 
[macg@localhost mysqltmp]$ ./tt
abc def hig
your input is :abc 
 

 
   gets能克服scanf的缺陷,读字符串包含空格
char a[30];

gets(a);
printf("your input is :%s/n",a);
 
[macg@localhost mysqltmp]$ ./tt
abc efd sdd
your input is :abc efd sdd
 


    fgets字符串指针改成字符串数组,消灭了Segmentation fault错误
char *re,*rec ;
re=fgets(rec,100 ,srcstream);   
出Segmentation fault错误
 
改成
char *re,rec[100];
re=fgets(rec,100, srcstream);   

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值