--- title: ポインタを使った演習も実践的になってきたよ author: kazu634 date: 2008-07-11 url: /2008/07/11/_1038/ wordtwit_post_info: - 'O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:4129;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}' categories: - C - Programming ---
『C言語ポインタ完全制覇 (標準プログラマーズライブラリ)』の勉強だよ。
/* ======================= */ /* === Include library === */ /* ======================= */ #include <stdio.h> #include <stdlib.h> /* ================= */ /* === Functions === */ /* ================= */ int main(void) { char buf[256]; int size; int *variable_array; int i; printf("Input Array Size: "); /* Read Data from STDIN */ fgets(buf, 256, stdin); sscanf(buf, "%d", &size); variable_array = malloc(sizeof(int) * size); for (i = ; i < size; ++i) { variable_array[i] = i; } for (i = ;i < size; ++i) { printf("variable_array[%d] : %d\n", i, variable_array[i]); } return ; }
/* ======================= */ /* === Include library === */ /* ======================= */ #include <stdio.h> #include <stdlib.h> #include <string.h> /* ================= */ /* === Functions === */ /* ================= */ void read_slogan(FILE *fp, char **slogan) { char buf[1024]; int i; for (i = ; i < 7; i++) { fgets(buf, 1024, fp); buf[strlen(buf) - 1] = '\0'; slogan[i] = malloc(sizeof(char) * (strlen(buf) + 1)); strcpy(slogan[i], buf); } } int main(void) { char *slogan[7]; int i; read_slogan(stdin, slogan); for (i = ; i < 7; ++i) { printf("%s\n", slogan[i]); } return ; }