我正在尝试动态增加int
数组的内存,但是我在让它工作时遇到了问题。它没有扩展并向数组添加更多元素,我不确定我做错了什么。请帮忙!
int* fibs = NULL;
void genFibs(){
int i = 1,curSize = 0,curNum = 0;
int flag = 1;
while(flag){
if(curSize != 0 &&curSize != 1){
curNum = fibs[curSize-2]+fibs[curSize-1];
}else if(curSize-1 == 1){
curNum = fibs[curSize-1]+fibs[curSize-1];
}else{
curNum = 1;
}
if(curNum<=10){
curSize++;
fibs = (int*)realloc(fibs,curSize*sizeof(int));
fibs[curSize-1] = curSize;
}else{
flag = 0;
}
}
}
}
void printFibs(){
int size = sizeof(fibs)/sizeof(int);
int i = 0;
for(i = 0;i<size;i++){
printf("%d is: %d\n",i,fibs[i]);
}
}