2012年1月27日金曜日

[C/C++] 新・明解C++入門編 8-1「文字列とポインタ」

書籍「新・明解C++入門編 (明解シリーズ)」の中に収められているプログラム等を実際にコンパイル,実行してみた際のメモです.

List 8-1
#include <iostream>
#include <typeinfo>

using namespace std;

int main()
{
  cout << " \"ABC\"\n";
  cout << "Type: " << typeid("ABC").name()
       << "Size: " << sizeof("ABC") << "\n\n";

  cout << " \"\"\n";
  cout << "Type: " << typeid("").name()
       << "Size: " << sizeof("") << "\n\n";

  cout << "\"ABC\\0DEF\"\n";
  cout << "Type: " << typeid("ABC\0DEF").name()
       << "Size: " << sizeof("ABC\0DEF") << "\n"

}
コンパイル,実行結果:
$ g++ List8-1.cpp
$ ./a.out
 "ABC"
Type: A4_cSize: 4

 ""
Type: A1_cSize: 1

"ABC\0DEF"
Type: A8_cSize: 8

List 8-2
#include <iostream>

using namespace std;

int main()
{
  char s[4];

  s[0] = 'A';
  s[1] = 'B';
  s[2] = 'C';
  s[3] = '\0'

  cout << "character array a\"" << s << "\" . \n";

}
コンパイル,実行結果:
$ g++ List8-2.cpp
$ ./a.out
character array a"ABC" . 

List 8-3

#include <iostream>

using namespace std;

int main()
{
  char s1[] = {'A', 'B', 'C', '\0'};
  char s2[] = {"ABC"};
  char s3[] = "ABC";

  cout << "Array s1\"" << s1 << "\"... \n";
  cout << "Array s2\"" << s2 << "\"... \n";
  cout << "Array s3\"" << s3 << "\"... \n"
}
コンパイル,実行結果:

$ g++ List8-3.cpp
$ ./a.out
Array s1"ABC"... 
Array s2"ABC"... 
Array s3"ABC"... 

List 8-4
#include <iostream>

using namespace std;

int main()
{
  char s[6] = "ABC";
  cout << "Array s \"" << s << "\"...\n"

}
コンパイル,実行結果:
$ g++ List8-4.cpp
$ ./a.out
Array s "ABC"...

List 8-5
#include <iostream>

using namespace std;

int main()
{
  char name[36];

  cout << "What is your name? : ";
  cin >> name;

  cout << "Hello, " << name << "!!\n"

}
コンパイル,実行結果:
$ g++ List8-5.cpp
$ ./a.out
What is your name? : Taro YAMADA
Hello, Taro!!
$ ./a.out
What is your name? : Taro_YAMADA
Hello, Taro_YAMADA!!

List 8-6
#include <iostream>

using namespace std;

void put_str(const char s[])
{
  for (int i = 0; s[i] != 0; i++)
    cout << s[i];
}

int main()
{
  char str[36];
  put_str("char: ");
  cin >> str;
  put_str(str);
  cout << '\n'

}
コンパイル,実行結果:
$ g++ List8-6.cpp
$ ./a.out
char: A
A
$ ./a.out
char: ABCDEFG
ABCDEFG

List 8-7
#include <cctype>
#include <iostream>

using namespace std;

void put_upper(const char s[])
{
  for (int i = 0; s[i]; i++)
    cout << static_cast<char>(toupper(s[i]));
}

int main()
{
  char str[36];
  cout << "Char : ";
  cin >> str;
  put_upper(str);
  cout << '\n'

}
コンパイル,実行結果:
$ g++ List8-7.cpp
$ ./a.out
Char : ABCDEFG
ABCDEFG

List 8-8
#include <iostream>

using namespace std;

int main()
{
  char str[] = "ABC";
  char* ptr  = "123";

  cout << "str = \"" << str << "\"\n";
  cout << "ptr = \"" << ptr << "\"\n";

}
コンパイル,実行結果:
$ g++ List8-8.cpp
List8-8.cpp:8:16: warning: conversion from string literal to 'char *' is deprecated
      [-Wc++11-compat-deprecated-writable-strings]
  char* ptr  = "123";
               ^
1 warning generated.
$ ./a.out
str = "ABC"
ptr = "123"
警告が出ますが,実行可能です.

List 8-9

#include <iostream>

using namespace std;

int main()
{
  char s[] = "ABC";
  cout << "s = \"" << s << "\"\n";
  s = "XYZ";
  cout << "s = \"" << s << "\""\n";
}
コンパイル,実行結果:
$ g++ List8-9.cpp
List8-9.cpp:9:5: error: array type 'char [4]' is not assignable
  s = "XYZ";
  ~ ^
List8-9.cpp:10:32: error: expected ';' after expression
  cout << "s = \"" << s << "\""\n";
                               ^
                               ;
List8-9.cpp:10:32: error: expected expression
List8-9.cpp:10:34: warning: missing terminating '"' character [-Winvalid-pp-token]
  cout << "s = \"" << s << "\""\n";
                                 ^
1 warning and 3 errors generated.
コンパイルエラーのため実行不可能です.

List 8-10

#include <iostream>

using namespace std;

int main()
{
  char* p = "ABC";
  cout << "p = \"" << p << "\"\n";
  p = "XYZ";
  cout << "p = \"" << p << "\"\n";
}
コンパイル,実行結果:
$ g++ List8-10.cpp
List8-10.cpp:7:13: warning: conversion from string literal to 'char *' is deprecated
      [-Wc++11-compat-deprecated-writable-strings]
  char* p = "ABC";
            ^
List8-10.cpp:9:7: warning: conversion from string literal to 'char *' is deprecated
      [-Wc++11-compat-deprecated-writable-strings]
  p = "XYZ";
      ^
2 warnings generated.
$ ./a.out
p = "ABC"
p = "XYZ"
警告は出ますが,実行可能です.

List 8-11
#include <iostream>

using namespace std;

int main()
{
  char a[][5] = {"LIPS", "C", "Ada"};
  char* p[] = {"PAUL", "X", "MAC"};

  for (int i = 0; i < 3; i++)
    cout << "a[" << i << "] = \"" << a[i] << "\"\n";
  for (int i = 0; i < 3; i++)
    cout << "p[" << i << "] = \"" << p[i] << "\"\n";

}
コンパイル,実行結果:
$ g++ List8-11.cpp
List8-11.cpp:8:16: warning: conversion from string literal to 'char *' is deprecated
      [-Wc++11-compat-deprecated-writable-strings]
  char* p[] = {"PAUL", "X", "MAC"};
               ^
List8-11.cpp:8:24: warning: conversion from string literal to 'char *' is deprecated
      [-Wc++11-compat-deprecated-writable-strings]
  char* p[] = {"PAUL", "X", "MAC"};
                       ^
List8-11.cpp:8:29: warning: conversion from string literal to 'char *' is deprecated
      [-Wc++11-compat-deprecated-writable-strings]
  char* p[] = {"PAUL", "X", "MAC"};
                            ^
3 warnings generated.
$ ./a.out
a[0] = "LIPS"
a[1] = "C"
a[2] = "Ada"
p[0] = "PAUL"
p[1] = "X"
p[2] = "MAC"
警告は出ますが,実行可能です.

0 件のコメント :

コメントを投稿