initialize char pointer c

Solutions on MaxInterview for initialize char pointer c by the best coders in the world

showing results for - "initialize char pointer c"
Oscar
06 Jan 2020
1//you should probably use dynamic memory allocation
2//this string is uninitialized and will cause errors
3char *str1; 
4//this string is initialized, but you would have to come up with text
5//I would highly discourage using this method
6char *str2 = "DummyTextThatFillsOutThisSpace";
7//this is dynamic memory allocation
8//You have to have a max size for the string
9//sizeof(dataType) returns the memory size of the data type
10//another reminder: ALWAYS free dynamically allocated memory
11int MaxSize = 10;
12char *str3 = malloc(MaxSize * sizeof(char));
13//code to use string
14//ALWAYS free dynamically allocated memory
15free(str3);