字符串

来源:互联网 发布:淘宝产品推广咋挣钱 编辑:程序博客网 时间:2024/04/30 21:58

字符串( character string )是以空字符(/0)结尾的char数组。

字符串常量(string constant),是指位于一对双引号中的任何字符。双引号里德字符加上编译器自动提供的结束标志/0字符,作为

一个字符串被存储在内存里。

运行结果:

Hi! I'm Clyde the Computer. I have many talents.
Let me tell you some of them.
What were they? Ah, yes, here's a partial list.
Adding numbers swiftly
Multiplying accurately
Stashing data
Following instructions to the letter
Understanding the C language

 

Enough about me -- what's your name?
Nigel Barntwit
Well, Nigel Barntwit, You must have many talents. Tell me some.
Limit yourself to one line's worth.
If you can't think of anything, fake it.
Fencing,yodeling,malingering,cheese tasting,and sighing.
Let's see if I've got that list:
Fencing,yodeling,malingering,cheese tasting,and sighing.
Thanks for the information, Nigel Barntwit.

 

 

如果想在字符串中使用双引号,可以在双引号前加一反斜杠

printf(" /" Run spot,run!/"exclaimed./n");

 

 

字符串数组

      初始化数组可以不用指定数组大小,编译器能通过查找空字符来确定字符串结束,但是定义数组时必须指定数组大小

const char m2[]="if you can't think of anything,fate it";

 

#define LINELEN 81

char name[LINELEN];

 

 

 

char heart[ ]="I love Tillie!";

char *head="I love Tillie!";

 

for(i=0;i<6;i++)                       for(i=0;i<6;i++)

     putchar(heart[i]);                     putchar(head[i]);

 

for(i=0;i<6;i++)                       for(i=0;i<6;i++)     

    putchar(*(heart+1));                 putchar(*(head+1)); 

但是只有指针可以使用增量运算符

while(*(head)!='/0')

         putchar(*(head++));

在数组形式中heart是个地址常量,改变heart意味着更改数组存储的位置,可以使用heart+1标识数组下一个元素。

 

 

const char *mytal={

                                   "Adding numbers swiftly",

                                   "Multiplying accurately",

                                   "Stashing data",

                                   "Following instructions to the letter",

                                   "Understand the C language"

                              }

mytal是一个由5个指向char的指针组成的数组。mytal是一个一维数组,而且数组里的每一个元素都是一个char类型值的地址。

*mytal[0]=='A'     *mystal[1]=='M'  *mytal[2]=='S'

mytal[0]= "Adding numbers swiftly"

可以把mytal[0]看作表示第一个字符串,并被看作是地址,*mytal[0]表示第一个字符串的第一个字符

原创粉丝点击