C-language dynamic array implementation

Source: Internet
Author: User

I. Overview

The C language cannot directly define a dynamic array, and the array must determine the length at initialization time.

If you want to determine the length of the array while the program is running, you need to go to the system to request a piece of memory to implement the dynamic array with dynamic memory allocation.

Two, dynamic memory allocation function 1, malloc () function
void *malloc(unsigned int size)

Allocates a size byte of memory space, returns a pointer to the address, and returns null if the memory is not well-divided. Note: The returned pointer is not of type, so it is used to force type conversion.

2. Calloc () function
void *calloc(unsigned int num, unsigned int size)

This is also the application of dynamic memory space, but it is separated. Apply a total of num bytes of memory space length to size byte.

3. Free () function
void free(void *p)

Releases the pointer p memory space.

This is very important!!! Very IMPORTANT!!! Important!!! 4. ReAlloc () function
void *realloc(void *p, unsigned int size)

The storage space requested for the pointer P is changed to size bytes, and the first address of the storage space (pointer) is returned.

Three, dynamic array implementation 1, one-dimensional dynamic array implementation

Dynamicarrayonedimensional.c

12345678910111213 14151617 181920 21222324 252627 28293031 323334 35363738     
#include <stdio.h>#include <stdlib.h>/**************************************************Dynamicarrayonedimensional.cEnter n number, averaging**************************************************/Main(){ Int*P=Null,N,I,Sum; Printf("Please enter array size:"); scanf("%d",&N); /* Request address n a contiguous storage space of sizeof (int) */ P=(Int*)Malloc(N*sizeof(Int)); If(P==Null) { Printf("No enough memory!\ n"); Exit(0); } Printf("Please enter the score:"); For(I=0;I<N;I++) { scanf("%d",P+I); } Sum=0; For(I=0;I<Ni++) { span class= "n" >sum = sum + * (p + i)  } printf ( " aver =%d\n " sum/ n); free (p }             /span>                
2, two-dimensional dynamic array implementation

Dynamicarraytwodimensional.c

123456789101112131415161718192021st22232425262728293031323334 35363738 394041 42434445 464748 49505152 535455 56575859     
#include <stdio.h>#include <stdlib.h>IntFindmax(Int*P,IntM,IntN,Int*PRow,Int*Pcol);IntMain(){ Int*Pscore=Null,I,J,M,N,Maxsocre,Row,Col; Printf("Please enter array size m,n:"); scanf("%d,%d",&M,&N); Pscore=(Int*)Calloc(M*N,sizeof(Int)); If(Pscore==Null) { Printf("No enough memory!\ n"); Exit(0); } Printf("Please enter the score:\ n"); For(I=0;I<M;I++) { For(J=0;J<N;J++) { scanf("%d",&Pscore[I*N+J]); } } Maxsocre=Findmax(Pscore,M,N,&Row,&Col); Printf("Maxsocre =%d, class =%d, Number =%d\ n",Maxsocre,Row+1,Col+1); Free(Pscore); Return0;}IntFindmax(Int*P,IntM,IntN,Int*PRow,Int*Pcol){ IntI,J,Max; Max=P[0]; *PRow=0; *Pcol=0; For(I=0;I<M;I++) { For(J=0;J<N;J++) { If(P[I*N+J]>Max)  { max = span class= "n" >p[i*n+ j *prow = i;< Span class= "line" > *pcol = j;}} } return  (max               

C language Dynamic array implementation

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.