4
22
2014
0

关于char *用法

关于一下

char * pChar;

if(NULL==(pChar=(char *)malloc(siziof(char)*200)))printf("malloc error\n")

memset((void*)pChar,0,200);//把200个字节内容清零

print("pChar[0]=%c",pChar[0]);

i=(pChar[0]==0;)printf("~~~%ld\n",i);

pChar[0]=1;

print("pChar[0]=%c",pChar[0]);

i=(pChar[0]==0;)printf("~~~%ld\n",i);

pChar[0]=48;

print("pChar[0]=%c",pChar[0]);

i=(pChar[0]==0;)printf("~~~%ld\n",i);

其实*pChar的内容置0,其实就是置为串尾符。

执行结果是:

由上面的结果也可以看出pChar=0其实是赋值NULL。清空

pChar=0,或者pChar=1,进行时直接的acsii值表的赋值。直接查国际Ascii表就可以知道结果了。比如数字0对应的ascii表值为48,则:

print("pChar[0]=%c",pChar[0]);的结果和ASCII码表相对应。故pChar[0]=48;则print("pChar[0]=%c",pChar[0]);打印的结果pChar[0]=0

 

 

故可以有如下操作

...

i=**;

if(pChar[i]==0)pChar[i]=1;

...

即虽然pChar[i]为char类型,但是可以直接赋值0,1.即按照ACSII码表来赋值

Category: 编程理解 | Tags: | Read Count: 1500

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com