嘿,我試圖轉換一個我寫的函數來生成一個long數組,它將Pascal的三角形代入一個返回mpz_t數組的函數中。但是用下面的代碼:用mpz_t製作pascal的三角形
mpz_t* make_triangle(int rows, int* count) {
//compute triangle size using 1 + 2 + 3 + ... n = n(n + 1)/2
*count = (rows * (rows + 1))/2;
mpz_t* triangle = malloc((*count) * sizeof(mpz_t));
//fill in first two rows
mpz_t one;
mpz_init(one);
mpz_set_si(one, 1);
triangle[0] = one; triangle[1] = one; triangle[2] = one;
int nums_to_fill = 1;
int position = 3;
int last_row_pos;
int r, i;
for(r = 3; r <= rows; r++) {
//left most side
triangle[position] = one;
position++;
//inner numbers
mpz_t new_num;
mpz_init(new_num);
last_row_pos = ((r - 1) * (r - 2))/2;
for(i = 0; i < nums_to_fill; i++) {
mpz_add(new_num, triangle[last_row_pos + i], triangle[last_row_pos + i + 1]);
triangle[position] = new_num;
mpz_clear(new_num);
position++;
}
nums_to_fill++;
//right most side
triangle[position] = one;
position++;
}
return triangle;
}
遇到錯誤說:不兼容的類型中的分配對於其中在三角形的位置被設定的所有行(即:三角形[位置] =一個)。
有誰知道我可能會做錯什麼?