fork download
  1. #include <string>
  2. #include <iostream>
  3.  
  4. int main()
  5. {
  6. // Because strings declared this way are immutable, you have
  7. // to use 'const' to declare that they cannot be modified.
  8. const char * name[] = { "pawan", "vaibhav", "sankalp", "love", "prakash", "shubham", "anshul", "lucky" };
  9.  
  10. // You want to swap two strings (that is, two pointers to char) in the
  11. // array of char pointers. So, you must pass the addresses of the array
  12. // elements that you want to exchange, that is, the addresses of the char pointers.
  13. swap(&name[1], &name[2]);
  14. std::cout << name[1]; std::cout << name[2];
  15. }
  16.  
  17. // Again you must use the 'const' keyword.
  18. void swap(const char **s1, const char **s2)
  19. {
  20. // The values at s1 and s2, that is, pointers to char,
  21. // are exchanged using the temporary char pointer t.
  22. const char *t;
  23. t = *s1;
  24. *s1 = *s2;
  25. *s2 = t;
  26. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:13:28: error: ‘swap’ was not declared in this scope
     swap(&name[1], &name[2]);
                            ^
prog.cpp:13:28: note: suggested alternatives:
In file included from /usr/include/c++/6/string:52:0,
                 from prog.cpp:1:
/usr/include/c++/6/bits/basic_string.h:5302:5: note:   ‘std::swap’
     swap(basic_string<_CharT, _Traits, _Alloc>& __lhs,
     ^~~~
In file included from /usr/include/c++/6/bits/stl_pair.h:59:0,
                 from /usr/include/c++/6/bits/stl_algobase.h:64,
                 from /usr/include/c++/6/bits/char_traits.h:39,
                 from /usr/include/c++/6/string:40,
                 from prog.cpp:1:
/usr/include/c++/6/bits/move.h:179:5: note:   ‘std::swap’
     swap(_Tp& __a, _Tp& __b)
     ^~~~
In file included from /usr/include/c++/6/exception:172:0,
                 from /usr/include/c++/6/new:40,
                 from /usr/include/c++/6/ext/new_allocator.h:33,
                 from /usr/include/x86_64-linux-gnu/c++/6/bits/c++allocator.h:33,
                 from /usr/include/c++/6/bits/allocator.h:46,
                 from /usr/include/c++/6/string:41,
                 from prog.cpp:1:
/usr/include/c++/6/bits/exception_ptr.h:164:5: note:   ‘std::__exception_ptr::swap’
     swap(exception_ptr& __lhs, exception_ptr& __rhs)
     ^~~~
stdout
Standard output is empty