We will share with you the code below. There are two methods to achieve this:
# Include
Int main () {char * src = "hello world"; int len = strlen (src); char * dest = (char *) malloc (len + 1 ); // allocate a memory space char * d = dest; char * s = & src [len-1]; // point to the last character while (len --! = 0) * d ++ = * s --; * d = 0; // Add '\ 0' printf ("% s \ n", dest) to the end ); free (dest); // The space should be released after use to avoid Memory leakage dest = NULL; // avoid generating a wild pointer return 0 ;}
Array Implementation:
# Include
# Include
Int main () {char str [] = "Hello world"; int len = strlen (str); char t; for (int I = 0; I
Differences between Heap and stack:
Heap is a stack, and stack is a stack.
Stack space is automatically allocated/released by the operating system, and Heap space is manually allocated/released.
Stack space is limited, and Heap is a large free storage zone.
The memory space allocated by the malloc function in C is on the heap, and the new operator corresponding to the C ++
During the compilation period, the program implements both the variable and function allocation memory on the stack, and the parameter transfer is also performed on the stack when the function is called during the program running.