Creation and use of one-dimensional dynamic arrays and two-dimensional dynamic arrays

Source: Internet
Author: User

#include <stdio.h>
#include <malloc.h>
void Main () {


int *a,n=10,i;
/*
The prototype of the Calloc () function is: (void *) calloc (unsigned n,unsigned size)
The Calloc () function is used to dynamically request to the system n, each memory unit that accounts for Sizege bytes, the function return value is the first address of the requested memory space
The main difference between malloc and calloc is that when the system's memory is only left with some very small fragments. The time efficiency of dynamic array design with calloc function is better than
The time efficiency of dynamic arrays designed with malloc function
*/
Dynamic request N-song int's memory space is indicated by pointer A to the first address
a= (int *) calloc (n,sizeof (int));
for (i=0;i<n;i++) {

a[i]=i+1;//array Element Assignment value
}


for (i=0;i<n;i++) {

printf ("%d", a[i]);//array element output
}


printf ("\ n");
Free (a);//dynamic release of the N-song memory space pointed to by pointer a

}







Creation and use of two-dimensional dynamic arrays

#include <stdio.h>
#include <malloc.h>




Functions to create a two-dimensional dynamic array
int **make2darray (int row,int col) {


Creates a two-dimensional dynamic array of row row col columns, and the function returns its first address
int **a,i;
Dynamic request row int type of memory space. Have a instruction
a= (int * *) calloc (row,sizeof (int *));
Cyclic row, dynamic request col int type of memory space, indicated by a[i]
for (i=0;i<row;i++) {

a[i]= (int *) calloc (col,sizeof (int));
}


return A;
}






Functions to release two-dimensional dynamic arrays
void Deliver2darray (int **a,int row) {
Frees the memory space of the two-dimensional dynamic array A, row number of rows array
int i;
for (i=0;i<row;i++) {

Free (a[i]);
}


Free (a);
}






void Main () {


/*
int *a,n=10,i;

The prototype of the Calloc () function is: (void *) calloc (unsigned n,unsigned size)
The Calloc () function is used to dynamically request to the system n, each memory unit that accounts for Sizege bytes, the function return value is the first address of the requested memory space
The main difference between malloc and calloc is that when the system's memory is only small fragments, the time efficiency of the dynamic array designed with the CALLOC function is better than
The time efficiency of dynamic arrays designed with malloc function

Dynamic request N-song int's memory space is indicated by pointer A to the first address
a= (int *) calloc (n,sizeof (int));
for (i=0;i<n;i++) {

a[i]=i+1;//array Element Assignment value
}


for (i=0;i<n;i++) {

printf ("%d", a[i]);//array element output
}


printf ("\ n");
Free (a);//dynamic release of the N-song memory space pointed to by pointer a
*/






Two-dimensional dynamic array
int i,j,c;
int row=3,col=4,**a;
A=make2darray (Row,col);
A=s;
for (i=0;i<row;i++) {

for (j=0;j<col;j++) {

a[i][j]=c;//array Element Assignment value
C + +;
}
}






for (i=0;i<row;i++) {

for (j=0;j<col;j++) {

printf ("%5d", A[i][j]);//display array elements
}


printf ("\ n");
}






Deliver2darray (A,row);
/*
The prototype of the ReAlloc () function is: (void *) realloc (void *p,unsigned size)
The function of the realloc () function is. Changes the size of the allocated memory area pointed to by the pointer p to size, and the function returns the newly allocated memory area
's first address. Size can be larger than the original allocated memory area, and can be smaller than the original allocated memory area.
The data value of the original memory area is saved as is in the newly allocated memory area. When the newly allocated memory area is set to size2 less than the original allocated memory area Size1
The first size2 data values in the original allocated memory area are saved as-is in the newly allocated memory area
*/

}
















Creation and use of one-dimensional dynamic arrays and two-dimensional dynamic arrays

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.