How to use arrays dynamically in C programs

Source: Internet
Author: User

C language does not allow the size of the array to be dynamically defined, it is not possible to temporarily enter the size of the arrays in the program, which brings some difficulties in programming.

The following is a small program that averages and standard deviations show how to dynamically define the size of an array in a program:

1 /*How to use arrays dynamically in C programs (Calloc functions)*/ 2#include <stdio.h>3#include <stdlib.h>4#include <math.h>5 intMain () {6     intNum//Number of data7     DoubleSUM,AVE,S2;//data and, average, variance8printf"Please enter the number of data:");9scanf"%d",&num); Ten     Double* Array= (Double*)calloc(Num,sizeof(Double));//Use the CALLOC function to request memory for an array of data and then receive it with a double pointer (you can also use the malloc function instead of calloc) Oneprintf"Please enter data:"); A      for(intI=0; i<num;i++) {//This allows data entry via a known array length -scanf"%LF",&array[i]); -sum+=Array[i]; the     } -      -printf"The average is:%lf", ave=sum/num); -      for(intI=0; i<num;i++){ +S2+=pow (Array[i]-ave,2); -     } +printf"The standard deviation is:%LF", sqrt (S2));//ask for standard deviation A      Free(array);//release the requested memory at     return 0; -}

The point is that the 10th line of code, to use the Calloc function: It is contained in the Stdlib.h header file, it needs to provide two parameters: ① object: That is, to define the size of the array; ② object length: In bytes, it is generally possible to use sizeof (type name) to obtain. The Calloc function then returns the "first address of the array", which we typically need to strongly convert to the type of array we need.

In this program, the average and standard deviation of an unknown number of data is required, first defining the INT variable num, which is the length of the array we have temporarily entered in the program, then we can call the Calloc function. Since we need a double array, we need to turn the return value of the Calloc function strong, and finally receive the first address with a double pointer, you can consider this pointer as an "array". You can then manipulate the array as you like.

The malloc function is similar to CALLOC except that it requires only one parameter: The total size of the address required, at which point we can pass the num*sizeof (double) to the malloc function. Calling the malloc function can either use stdlib.h or use malloc.h.

How to use arrays dynamically in C programs

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.