在我試圖理解malloc和結構我所遇到的一個錯誤,我不明白的malloc數組給分段故障
#include <stdio.h>
#include <stdlib.h>
typedef struct match
{
int round;
} match;
void foo(match *matches) {
for(int i = 0; i < 10; i++) {
matches = (match *) realloc(matches, i + 1);
matches[i].round = i + 1;
}
}
int main()
{
match *matches;
matches = (match *) malloc(0);
foo(matches);
free(matches);
return(0);
}
所以在我試圖填補這一陣列的比賽dynamicaly失敗
爲什麼的malloc(0)? –
'matches =(match *)realloc(matches,i + 1);'你想要這行嗎? – Danh