Inverted string output in C

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.