5.5 文字ポインタと関数, 演習5-3 K&R プログラミング言語C

5.5 文字ポインタと関数

以下のように文字列定数の内容を書き換えようとすると失敗する。

#include <stdio.h>

int main(void)
{
    char amessage[] = "now is the time"; /* 配列 */
    char *pmessage = "now is the time";  /* ポインタ */

    printf("amessage[] => %s\n", amessage);
    printf("*pmessage => %s\n", pmessage);

    printf("----------\n");

    amessage[0] = 'N'; /* 配列版の先頭文字を書き換える。 */
    printf("amessage[] => %s\n", amessage);

    *pmessage = 'N'; /* ポインタ版の先頭文字を書き換える。 */
    printf("*pmessage => %s\n", pmessage);

    return 0;
}

実行結果

$ ./str_test
amessage[] => now is the time
*pmessage => now is the time
----------
amessage[] => Now is the time
Bus error

ポインタの指し示す先を配列版の先頭に変更すると、ポインタ版の文字の変更は配列版の変更となる。

#include <stdio.h>

int main(void)
{
    char amessage[] = "now is the time"; /* 配列 */
    char *pmessage = "now is the time";  /* ポインタ */

    printf("amessage[] => %s\n", amessage);
    printf("*pmessage => %s\n", pmessage);

    printf("----------\n");

    pmessage = &amessage[0]; /* ポインタを配列版の文字列を指し示すように変更する。 */
    *pmessage = 'N'; /* 先頭文字を書き換える。 => 配列版の文字列が書き換えられる。 */
    printf("amessage[] => %s\n", amessage);
    printf("*pmessage => %s\n", pmessage);

    return 0;
}

実行結果

$ ./str
amessage[] => now is the time
*pmessage => now is the time
----------
amessage[] => Now is the time
*pmessage => Now is the time

演習5-3

#include <stdio.h>
#include <string.h> /* strcpy を使うため */

void my_strcat(char*, char*);
void my_strcat2(char*, char*);

int main(void)
{
    char s1[] = "hello";
    char s2[] = ", world.";
    char s3[] = "from ";
    char s4[] = "Japan.";

    my_strcat(s1, s2);
    printf("my_strcat(s1, s2); => %s\n", s1);

    my_strcat2(s3, s4);
    printf("my_strcat2(s3, s4); => %s\n", s3);

    return 0;
}

void my_strcat(char *s, char *t)
{
    /* s の指し示す文字が真の間(終端文字の'
#include <stdio.h>
#include <string.h> /* strcpy を使うため */

void my_strcat(char*, char*);
void my_strcat2(char*, char*);

int main(void)
{
    char s1[] = "hello";
    char s2[] = ", world.";
    char s3[] = "from ";
    char s4[] = "Japan.";

    my_strcat(s1, s2);
    printf("my_strcat(s1, s2); => %s\n", s1);

    my_strcat2(s3, s4);
    printf("my_strcat2(s3, s4); => %s\n", s3);

    return 0;
}

void my_strcat(char *s, char *t)
{
    /* s の指し示す文字が真の間(終端文字の'\0'に達するまで)ポインタをインクリメントする。 */
    while (*s)
        s++;
    /* *t を *s に代入し、代入した文字が終端文字'\0'でなければ、それぞれのポインタをインクリメントしてループを繰り返す。 */
    while (*s++ = *t++)
        ;
}

/* strcpy を使った版。while の条件節を少しわかりやすくした。 */
void my_strcat2(char *s, char *t)
{
    while (*s != '\0')
        s++;
    strcpy(s, t); /* strcpy が開始される時点で s は '\0' を指している。 */
}
'に達するまで)ポインタをインクリメントする。
*/ while (*s) s++; /* *t を *s に代入し、代入した文字が終端文字'
#include <stdio.h>
#include <string.h> /* strcpy を使うため */

void my_strcat(char*, char*);
void my_strcat2(char*, char*);

int main(void)
{
    char s1[] = "hello";
    char s2[] = ", world.";
    char s3[] = "from ";
    char s4[] = "Japan.";

    my_strcat(s1, s2);
    printf("my_strcat(s1, s2); => %s\n", s1);

    my_strcat2(s3, s4);
    printf("my_strcat2(s3, s4); => %s\n", s3);

    return 0;
}

void my_strcat(char *s, char *t)
{
    /* s の指し示す文字が真の間(終端文字の'\0'に達するまで)ポインタをインクリメントする。 */
    while (*s)
        s++;
    /* *t を *s に代入し、代入した文字が終端文字'\0'でなければ、それぞれのポインタをインクリメントしてループを繰り返す。 */
    while (*s++ = *t++)
        ;
}

/* strcpy を使った版。while の条件節を少しわかりやすくした。 */
void my_strcat2(char *s, char *t)
{
    while (*s != '\0')
        s++;
    strcpy(s, t); /* strcpy が開始される時点で s は '\0' を指している。 */
}
'でなければ、それぞれのポインタをインクリメントしてループを繰り返す。
*/ while (*s++ = *t++) ; } /* strcpy を使った版。while の条件節を少しわかりやすくした。 */ void my_strcat2(char *s, char *t) { while (*s != '
#include <stdio.h>
#include <string.h> /* strcpy を使うため */

void my_strcat(char*, char*);
void my_strcat2(char*, char*);

int main(void)
{
    char s1[] = "hello";
    char s2[] = ", world.";
    char s3[] = "from ";
    char s4[] = "Japan.";

    my_strcat(s1, s2);
    printf("my_strcat(s1, s2); => %s\n", s1);

    my_strcat2(s3, s4);
    printf("my_strcat2(s3, s4); => %s\n", s3);

    return 0;
}

void my_strcat(char *s, char *t)
{
    /* s の指し示す文字が真の間(終端文字の'\0'に達するまで)ポインタをインクリメントする。 */
    while (*s)
        s++;
    /* *t を *s に代入し、代入した文字が終端文字'\0'でなければ、それぞれのポインタをインクリメントしてループを繰り返す。 */
    while (*s++ = *t++)
        ;
}

/* strcpy を使った版。while の条件節を少しわかりやすくした。 */
void my_strcat2(char *s, char *t)
{
    while (*s != '\0')
        s++;
    strcpy(s, t); /* strcpy が開始される時点で s は '\0' を指している。 */
}
'
) s++; strcpy(s, t); /* strcpy が開始される時点で s は '
#include <stdio.h>
#include <string.h> /* strcpy を使うため */

void my_strcat(char*, char*);
void my_strcat2(char*, char*);

int main(void)
{
    char s1[] = "hello";
    char s2[] = ", world.";
    char s3[] = "from ";
    char s4[] = "Japan.";

    my_strcat(s1, s2);
    printf("my_strcat(s1, s2); => %s\n", s1);

    my_strcat2(s3, s4);
    printf("my_strcat2(s3, s4); => %s\n", s3);

    return 0;
}

void my_strcat(char *s, char *t)
{
    /* s の指し示す文字が真の間(終端文字の'\0'に達するまで)ポインタをインクリメントする。 */
    while (*s)
        s++;
    /* *t を *s に代入し、代入した文字が終端文字'\0'でなければ、それぞれのポインタをインクリメントしてループを繰り返す。 */
    while (*s++ = *t++)
        ;
}

/* strcpy を使った版。while の条件節を少しわかりやすくした。 */
void my_strcat2(char *s, char *t)
{
    while (*s != '\0')
        s++;
    strcpy(s, t); /* strcpy が開始される時点で s は '\0' を指している。 */
}
' を指している。
*/ }

実行結果

$ ./ex5-3
my_strcat(s1, s2); => hello, world.
my_strcat2(s3, s4); => from Japan.
プログラミング言語C 第2版 ANSI規格準拠
B.W. カーニハン D.M. リッチー
共立出版
売り上げランキング: 9726
«
»