C言語メモ

「独習C」から印象的だったものを。

#include <stdio.h>
#include <ctype.h>

void string_up(char *p);

int main(void) 
{
    char str[] = "This is a test.";
    string_up(str);
    printf(str);
    return 0;
}

void string_up(char *p) 
{
    while (*p) {
        *p = toupper(*p);
        p++;
    }
}

これを実行すると、

THIS IS A TEST.

が出力される。

while文を *p で制御しているあたりに惚れた。

文字列、ポインタの扱い方が勉強になるコード。