1. 程式人生 > >C語言動態建立陣列

C語言動態建立陣列

通過使用指標以及malloc函式來分配地址空間

	#include<stdio.h>
	#include<malloc.h>
	void array(unsigned int i);
	void main(){
	array(10);
	}

	void array(unsigned int i)
	{
		unsigned int *arr;//指標用於指向陣列的首地址
		unsigned int j=0;
		arr=(unsigned int *)malloc(sizeof(unsigned int)*i);//分配陣列地址空間
		for(j=i;j>0;j--)
			arr[j-1]=j-1;
		for(j=i;j>0;j--)
			printf("%d,",arr[j-1]);
		printf("\b \n");
	}