LoginSignup
2
2

More than 5 years have passed since last update.

C言語入門~文字列とポインタ~

Posted at

ポインタによる文字列

ポインタによる文字列は以下のように宣言・初期化する.
char *p = "String";

ポインタpと文字列リテラル"String"のそれぞれが記憶域を占有する.
ポインタpは文字列の先頭文字を指すポイント.
ちなみに,配列名も先頭文字を指します.

文字列の配列

char *p[] = {"C","JavaScript","swift"};

文字列を扱うライブラリ

関数 機能
strlen(s); 文字列の長さを求める
strcpy(s1,s2); s2の文字列をs1にコピーする
strncpy(s1,s2,n); strcpyにコピーする文字数に制限を設ける
strcat(s1,s2); 文字列を連結する
strncat(s1,s2,n); strcatに連結する文字数に制限を設けることが可能
strcmp(s1,s2); 文字列の大小関係を求める.s1がs2より大きければ正の整数値,等しければ0,s1よりs2小さければ負の整数
atoi(nptr) nptrが指す文字列を,int型の表現に変換する.
atol(nptr) nptrが指す文字列を,long型の表現に変換する.
atof(nptr) nptrが指す文字列を,double型の表現に変換する.
2
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2