c primer plus----第11章文字列と文字列関数(一)

7543 ワード

11.1.1プログラムで文字列1を定義する.文字列定数2を使用する.char配列3.charポインタ4.文字列配列
//11.1 //         \0   char   //gets() puts()   

#include <stdio.h>

#define MSG "You must have many talents.Tell me some."



#define LIM 5

#define LINELEN 81



int main(void) { char name[LINELEN]; char talents[LINELEN]; int i; //      5    //#define MSG "You must have many talents.Tell me some."

    const char m1[40] = "Limit yourself to one line worth."; const char m2[] = "if you can not think of anything ,fake it."; const char *m3 = "
Enough about me - what is your name?
"; // :const char *m4[] = "
Enough about me - what is your name?";
const char *mytal[LIM] = { "Adding numbers swiftly", "Multiplying accurately", "Stashing data", "Following instructions to the letter", "Understanding the C language" }; printf("Hi! I am Clyde the Computer." "I have many talents.
"); printf("Let me tell you some of them.
"); puts("What were they? here is a partial list."); for(i = 0; i < LIM; i++) puts(mytal[i]); puts(m3); gets(name); printf("Well, %s, %s
", name, MSG); printf("%s
%s
", m1, m2); gets(talents); puts("Let see if i have got that list:"); puts(talents); printf("Thanks for the information, %s.
", name); return 0; }

一.文字列定数(文字列文字)1.二重引用符の文字にコンパイラが自動的に提供する終了フラグ0文字を付けます.    2.#defineは、文字列定数を定義するためにも使用できます.    3.以下等しい:char greeting[50]=「Hello」「World!」      char greeting[50] = "Hello World!";    4.文字列は静的ストレージクラスです.静的ストレージとは、1つの関数で文字列定数を使用する場合、この関数が複数回呼び出されても、プログラムの実行中に1部しか格納されない文字列です.    5.引用符全体の内容は、文字列の格納場所を指すポインタとして使用されます.配列名を配列格納位置を指すポインタと同様である.
//11.2

#include<stdio.h>

int main(void) { printf("%s,%p,%c
", "We", "are", *"coder!"); //%p //*"coder!" , "coder!" //We,0x80484fa,c return 0; }

二.文字列配列および初期化1.const char m1[] = {'H','e','l','l','o','\0',};1)0がない場合は文字列ではなく文字配列です.2)コンパイラは配列の大きさを決定する.    2.配列サイズを指定する場合は、配列要素が文字列の長さより少なくとも1多いことを確認し、使用されていない要素が自動的に0に初期化されます.    3.配列を宣言する場合、配列サイズは、実行時に得られる変数の値ではなく、整数定数でなければなりません.コンパイル時に配列のサイズがプログラムにロックされます.      int n = 8;      char m2[n];//C 99以降は長くなる配列で、それまでは無効です.      char m3[2 + 5];//合法4.m1 == &m1[0];      *m1 == 'H';      *(m1 + 1) == m1[1]=='e';    5.次の2つは、m 3が所与の文字列を指すポインタであることを宣言する.      const char m3[] = "Enough about me - what is your name?";      const char *m3 = "Enough about me - what is your name?";