Advertisement
193030

Dynamic string size

Jul 19th, 2020
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.48 KB | None | 0 0
  1. // Example program
  2. #include <stdio.h>
  3. #include <string>
  4. #include <stdlib.h>
  5. using namespace std;
  6. int main()
  7. {
  8.   int sizeOfString = 0;
  9.   printf("Enter size: \n");
  10.   scanf("%d", &sizeOfString);
  11.  
  12.   char* string = (char*)malloc(sizeOfString);
  13.   for(int i =0; i<sizeOfString; i++)
  14.   {
  15.       string[i] = rand()%26+'a';
  16.   }
  17.   string[sizeOfString] = 0;
  18.   printf("%s\n", string);
  19.   int stringSize = sizeof(string);
  20.   printf("size of string %d \n", stringSize);
  21.  
  22.   return 0;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement