0

ggc 给了我这个错误,我无法相信这个错误是正确的,因为输入和预期参数都是 [8][8] 指向数组的指针,知道这里有什么问题!

main.c: In function ‘main’:main.c:24:3: warning: passing argument 1 of ‘DRESC_Transform’ from incompatible pointer type [enabled by default]
In file included from main.c:1:0:
global.h:13:6: note: expected ‘short int * (*)[8]’ but argument is of type ‘short int (*)[8][8]’

这是 global.h

#include <math.h>
#include <stdio.h>

# define ROW 8
# define COL 8
# define SHIFT_AMOUNT 16
# define SHIFT_MASKT ( 1<<SHIFT_AMOUNT )-1


void DRESC_Transform( short *matrix [ROW][COL]);


void DRESC_Transform( short *matrix [ROW][COL]){


  int _tmp_matrix [ROW*COL]; // 2KB REGISTER 

  for ( unsigned i = 0; i <ROW; i++){ 

    int _tmp0, _tmp1, _tmp2, _tmp3, _tmp4, _tmp5, _tmp6, _tmp7;

    _tmp0 = *matrix [i][0];
    _tmp1 = *matrix [i][1];
    _tmp2 = *matrix [i][2];
    _tmp3 = *matrix [i][3];
    _tmp4 = *matrix [i][4];
    _tmp5 = *matrix [i][5];
    _tmp6 = *matrix [i][6];
    _tmp7 = *matrix [i][7];

    _tmp_matrix [    (i<<3)] = _tmp0 <<SHIFT_AMOUNT;
    _tmp_matrix [1 + (i<<3)] = _tmp1 <<SHIFT_AMOUNT;
    _tmp_matrix [2 + (i<<3)] = _tmp2 <<SHIFT_AMOUNT;
    _tmp_matrix [3 + (i<<3)] = _tmp3 <<SHIFT_AMOUNT;
    _tmp_matrix [4 + (i<<3)] = _tmp4 <<SHIFT_AMOUNT;
    _tmp_matrix [5 + (i<<3)] = _tmp5 <<SHIFT_AMOUNT;
    _tmp_matrix [6 + (i<<3)] = _tmp6 <<SHIFT_AMOUNT;
    _tmp_matrix [7 + (i<<3)] = _tmp7 <<SHIFT_AMOUNT;

  }

  for ( unsigned i = 0; i < ROW; i++){ // a row calculation

    int _tmp;

    _tmp_matrix[    (i<<3)] = ( _tmp_matrix[    (i<<3)] + _tmp_matrix[1 + (i<<3)] ) >>1;
    _tmp                    = ( _tmp_matrix[    (i<<3)] - _tmp_matrix[1 + (i<<3)] );
    _tmp_matrix[1 + (i<<3)] = ( _tmp_matrix[2 + (i<<3)] + _tmp_matrix[3 + (i<<3)] ) >>1;
    _tmp_matrix[2 + (i<<3)] = ( _tmp_matrix[4 + (i<<3)] + _tmp_matrix[5 + (i<<3)] ) >>1;
    _tmp_matrix[4 + (i<<3)] =   _tmp;
    _tmp                    = ( _tmp_matrix[6 + (i<<3)] + _tmp_matrix[7 + (i<<3)] ) >>1;
    _tmp_matrix[6 + (i<<3)] = ( _tmp_matrix[2 + (i<<3)] - _tmp_matrix[5 + (i<<3)] );
    _tmp_matrix[5 + (i<<3)] = ( _tmp_matrix[1 + (i<<3)] - _tmp_matrix[3 + (i<<3)] );
    _tmp_matrix[3 + (i<<3)] =   _tmp;
    _tmp_matrix[7 + (i<<3)] = ( _tmp_matrix[3 + (i<<3)] - _tmp_matrix[7 + (i<<3)] );
  }


  for ( unsigned i = 0; i <ROW; i++){

    int _tmp0, _tmp1, _tmp2, _tmp3, _tmp4, _tmp5, _tmp6, _tmp7;

    _tmp0 = _tmp_matrix [    (i<<3)] >>SHIFT_AMOUNT;
    _tmp1 = _tmp_matrix [1 + (i<<3)] >>SHIFT_AMOUNT;
    _tmp2 = _tmp_matrix [2 + (i<<3)] >>SHIFT_AMOUNT;
    _tmp3 = _tmp_matrix [3 + (i<<3)] >>SHIFT_AMOUNT;
    _tmp4 = _tmp_matrix [4 + (i<<3)] >>SHIFT_AMOUNT;
    _tmp5 = _tmp_matrix [5 + (i<<3)] >>SHIFT_AMOUNT;
    _tmp6 = _tmp_matrix [6 + (i<<3)] >>SHIFT_AMOUNT;
    _tmp7 = _tmp_matrix [7 + (i<<3)] >>SHIFT_AMOUNT;

    *matrix [i][0] = _tmp0;
    *matrix [i][1] = _tmp1;
    *matrix [i][2] = _tmp2;
    *matrix [i][3] = _tmp3;
    *matrix [i][4] = _tmp4;
    *matrix [i][5] = _tmp5;
    *matrix [i][6] = _tmp6;
    *matrix [i][7] = _tmp7;


  } 

}

这是主要的:

#include "global.h"

void main (){

  short matrix [ROW][COL] = {{7, 1, 6, 6, 3, -5, 4, 2}, // <<<<---
                 {7, 1, 6, 6, 3, -5, 4, 2},
                 {7, 1, 6, 6, 3, -5, 4, 2},
                 {7, 1, 6, 6, 3, -5, 4, 2},
                 {7, 1, 6, 6, 3, -5, 4, 2},
                 {7, 1, 6, 6, 3, -5, 4, 2},
                 {7, 1, 6, 6, 3, -5, 4, 2},
                 {7, 1, 6, 6, 3, -5, 4, 2}};

  DRESC_Transform(&matrix); // <<<---

  printf("the transformed matrix is \n");
  printf("{");
  for ( unsigned i; i <ROW; i++){
    for ( unsigned j = 0; j <COL; j++){
      printf( "%d,", matrix[i][j]);
    }
    printf("\b}, \n");
  }
  printf("\b} \n");

}
4

1 回答 1

0

一般来说,对于任何维度的数组:

T array[S1][S2][...][SN]

当你将它传递给一个函数时,衰减到

T array[][S2][...][SN]

这只不过是一种不同的语法

T (*array)[S2][...][SN]

因此,如果要将数组传递short matrix[ROW][COL]给函数,函数应该接受short matrix[][COL]

从语法中可以看出T (*array)[S2][...][SN],数组作为指针传递。所以不要害怕;如果您在函数内部修改数组的元素,则当该函数返回时,结果将在原始数组中可见。

唯一一次看不到赋值的情况是,如果修改指针matrix(函数中的局部变量),然后直接将其赋值为指向新数组,例如matrix = ....


至于其他注释,void main不是main. 应该是int main,如果你使用的是 C,你应该return 0在最后。

于 2013-03-12T04:03:01.100 回答