Advertisement
bhok

C++ Dynamic Declare

Oct 2nd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.53 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <stdio.h>
  4. #include <string.h>
  5.  
  6. #pragma warning(disable:4996)
  7.  
  8. int main()
  9. {
  10.    
  11.  
  12.     // static
  13.     char str1[11];
  14.     str1[10] = '\0';
  15.     strcpy(str1, "abcdefghij");
  16.  
  17.     // dynamic
  18.     // str2 is now considered a char*
  19.     char* str2;
  20.     str2 = new char[11];
  21.     // str2 is now converted to a dynamic array of char
  22.     // for 11 elements
  23.     str2[10] = '\0';
  24.     // we declare the element 10 to have a null-terminator
  25.     strcpy(str2, str1);
  26.  
  27.     std::cout << str1 << std::endl << str2;
  28.    
  29.     system("pause");
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement