void print_first_n_row(double **matrix, int n, int row_size) {
double (*abc)[row_size];
abc=matrix;}
我遇到了"从不兼容的指针类型转换导致的赋值问题 [-Wincompatible-pointer-types]"的错误,怎样解决?
void print_first_n_row(double **matrix, int n, int row_size) {
double (*abc)[row_size];
abc=matrix;}
我遇到了"从不兼容的指针类型转换导致的赋值问题 [-Wincompatible-pointer-types]"的错误,怎样解决?
"我遇到了'assignment from incompatible pointer type [-Wincompatible-pointer-types] abc=matrix'错误。如何解决?"
double (*abc)[row_size];
- abc
是一个指向 double
数组的指针。
double **matrix
- matrix
是一个指向指向 double
的指针。
存在类型不匹配问题。 *
vs. **
。
不能将 指向指向 double
的指针 的值 赋给 指向 double
数组的指针。
将 double (*abc)[row_size];
改为 double **abc
或 double (**abc)[row_size];
,或者如果 abc
不需要,则直接使用 matrix
。
好的,答案是matrix
和abc
必须以相同的方式声明。你需要改变原型为:
void print_first_n_row(int n, int row_size, double (*matrix)[row_size]) // row_size needs to be defined first
{
double (*abc)[row_size] = matrix;
...
}
或者您需要更改abc
的定义为
double **abc;
这取决于你如何定义参数matrix
,以及如何调用print_first_n_row
函数。
假设你的代码类似于:
int main( void )
{
int rows = ...; // assumes rows and cols are set at runtime
int cols = ...;
...
double matrix[rows][cols];
...
print_first_n_rows( matrix, n, cols )
...
}
print_first_n_rows( n, cols, matrix );
in array[4][2] = {{1,2,3,4},{5,6,7,8}};
存储在内存中作为单个连续的位置块:
|1|2|3|4|5|6|7|8|
|1|2|3|4|
|5|6|7|8|
int main(void)
{
print_first_n_row(4,2);// params to create 4X2 array
return 0;
}
void print_first_n_row(int r, int c)
{
double matrix[r][c]; //create array using Variable Length Array
double *abc = &matrix[0][0];//create pointer of same base type
//and set address to pointer to point to
//first location of matrix
printf("%s", "array contents:");
for(int i=0; i<r*c; i++)
{
*(abc + i) = i*i;
printf("%0.2f,", *(abc + i));
}
}
void print_first_n_row(double matrix[4][2]);
int main(void)
{
double array1[4][2] = {1,2,3,4,5,6,7,8};
print_first_n_row(array1);
return 0;
}
void print_first_n_row(double matrix[4][2])
{
double *abc = &matrix[0][0];//create pointer of same base type
//and set address to pointer to point to
//first location of matrix
...(same as first example)
void print_first_n_row(double **matrix, int n, int row_size)
修改为void print_first_n_row(int n, int row_size, double (*matrix)[row_size])
,并相应地修改调用者?(这取决于函数和abc
的使用方式) - MikeCATprint_first_n_row
函数的代码,并展示对应matrix
参数是如何声明的。 - John Bode