Malloc dynamically allocates memory and malloc dynamically allocates memory.

Source: Internet
Author: User

Malloc dynamically allocates memory and malloc dynamically allocates memory.

I haven't studied C for a long time. After reviewing it, sometimes I feel that C is very elegant, and I want to learn the best choice of algorithms and data structures.

# Include "stdafx. h "# include <stdlib. h> int main () {int arr [5] = {1, 2, 3, 4, 5}; int len; printf ("Enter the array length len = "); scanf_s ("% d", & len); int * pArr = (int *) malloc (sizeof (int) * len); // 1. apply for 20 bytes of memory for read/write. // 2. the malloc function returns only the first byte address, all of which must be forcibly converted to int * // * pArr = 4; // similar to arr [0] = 4; // pArr [1] = 3; // similar to arr [1] = 3; printf ("Enter % d numbers \ r \ n", len ); for (int I = 0; I <len; I ++) {scanf_s ("% d", & pArr [I]);} printf ("the five numbers you entered are \ r \ n"); for (int j = 0; j <len; j ++) {printf ("% d \ r \ n", * (pArr + j);} free (pArr); // release the applied memory return 0 ;}

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.