Guest User

Untitled

a guest
Jan 14th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. //#define _GRIBCXX_DEBUG
  2. #include <bits/stdc++.h>
  3. # define rep(i, n) for (int i = 0; i < (int)(n); i++)
  4. using namespace std;
  5.  
  6. template<typename integer>
  7. integer __lcm(integer a, integer b) {
  8. return (a * b) / __gcd(a, b);
  9. }
  10.  
  11.  
  12. void swap(int *a, int *b) {
  13. int x;
  14. x = *a;
  15. *a = *b;
  16. *b = x;
  17. }
  18.  
  19. // 代入を使ってループをしてコピー
  20. void strcpy(char *s, char *t) {
  21. while (*s++ = *t++)
  22. ;
  23. }
  24.  
  25. int main() {
  26. int a = 2;
  27. int b = 4;
  28.  
  29. // swap の実装
  30. cout << a << "," << b << endl;
  31. // 引数がポインタ型のときは勝手にキャストされる?
  32. swap(a, b);
  33. swap(&a, &b);
  34. cout << a << "," << b << endl;
  35.  
  36. // 配列をポインタで
  37. int c[3] = {1, 2, 3};
  38. // int *i = &c[0];
  39.  
  40. // []がないと,配列の先頭要素へのポインタという意味になる
  41. int *i = c;
  42. cout << c << endl;
  43.  
  44. cout << *i << endl;
  45. i++;
  46. cout << *i << endl;
  47.  
  48. char d[] = "Sample string";
  49. char e[sizeof(d)];
  50.  
  51. cout << "d: " << d << endl;
  52. cout << "e: " << e << endl;
  53. strcpy(e, d);
  54. // これがエラーになってしまうのはなぜ?
  55. // no matching function for call to 'strcpy(char (*)[14], char (*)[14])'
  56. // strcpy(&e, &d);
  57. cout << "e: " << e << endl;
  58.  
  59. return 0;
  60. }
Add Comment
Please, Sign In to add comment