#include <stdlib.h>
#include <stdio.h>
#define SIZE 20
int *p ;
int m = SIZE;
int n = 0;//多少個元素
int main(){
p = malloc(SIZE);
scan_input();
printArray();
free(p);
return 0;
}
void scan_input(){
//scanf("%d",p[]);
int i =0;
while(1){
if(i>=m/4){//int 類型所以除以4
//relloc
m = m*2;//一次性擴大兩倍
printf("%s","realloc...");
p = realloc(p,m);
}
scanf("%d",&p[i]);
if(p[i] == -1){
break;
}
i++;
n++;
}
return ;
}
void printArray(){
int i;
for(i=0;i<n;i++){
printf("%d\n",p[i]);
}
return ;
}
realloc動態數組以及作用域
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.