5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【C++】char型ポインタの備忘録

Last updated at Posted at 2018-09-26

C++のポインタについての備忘録


char *p; // ポインタ
char str[] = "abcde";

charポインタとchar配列を用意しておく.

・ポインタに配列の先頭のアドレスを格納する.

p = str;
// または
p = &str[0];

・配列の参照
char配列自体の呼び出しパターン

std::cout << str <<endl; 
// abcde
std::cout << str[3] <<endl;
// d
std::cout << &str <<endl;
// 0x555d49680260
std::cout << &str[0] <<endl;
// 0x555d49680260

・ポインタの参照
charポインタの呼び出しパターン

std::cout << *p <<endl; // 先頭のアドレスにある要素
// a
std::cout << p[3] <<endl; // 3番目のアドレスにある要素
// d
std::cout << p <<endl; // char配列全体
// abcde
std::cout << &p <<endl;  // 先頭のアドレス
// 0x555d49680260
std::cout << &p+1 <<endl; //先頭の次のアドレス
// 0x555d49680261

int配列などとは違い,
配列の名前自体(ここではstr)を呼び出すとアドレスが返ってこず要素全体が返ってくる.
覚えておこう!

5
5
4

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
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?