C > 文字列
更新日
2014-11-11
広告
C言語で文字列を扱う方法を紹介します。
数値から文字列への変換
数値から文字列へ変換するには、sprintfを使います。#include <stdio.h> int id = 243; char str_id[32]; sprintf(str_id, "%d", id);これで、変数str_idに243が格納されます。
文字列のコピー
文字列をコピーする方法を紹介します。 文字列のコピーにはstrcpyを使います。#include <string.h> char hoge[] = "afseasfe"; char *target; target = malloc(strlen(hoge) + 1); strcpy(target, hoge);変数hogeの文字列を、変数targetにコピーします。 mallocで+1しているのは、strcpyがコピーの際に、文字列の末尾に'\0'を付加するためです。
広告
お問い合わせは sweng.tips@gmail.com まで。