'Can't realloc character string in C
I'm having some issues with realloc, for example, this code
char *s = (char*)malloc(sizeof(char)*17);
s = "bruhbruhlmaolmao";
s = (char*)realloc(s, sizeof(char)*18);
if(s == NULL) printf("Memory allocation failed");
else printf("%s\n",s);
I'm trying to reallocate the memory chunk that s is pointing at to a larger memory location in the heap.
I've compiled it and the compiler returns no warning. When I run the executable, It prints out nothing. I hope you can explain to me why and how to fix it
Solution 1:[1]
Well, apparently I made a dumb decision by assigning s to a string: s = "bruhbruhlmaolmao";, what I should really do is use something in string.h library like strcpy
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Hibiki Supersanta |
