【问题标题】:Subscripted value is neither array nor pointer nor vector in C programC程序中的下标值既不是数组也不是指针也不是向量
【发布时间】:2020-12-01 01:06:39
【问题描述】:

我有一个程序传递了一个大小为 448x448 的 img_ptr,它被分成 4 个相等的部分,每个角都应用了过滤器,现在我正在尝试使用每个存储在 subBlock 中的 4 个部分将图像重新组合在一起子块列表[];

我被抛出这个错误: “错误:下标值既不是数组也不是指针也不是向量” 对于每一行:“subImageTempHolder[i][j] = subBlockList[x].vertMask[i][j]

我该如何纠正这个问题,我怀疑它与指针有关。

子块结构

struct subBlock {
    unsigned char *horizMask;
    unsigned char *vertMask;
    unsigned char *combMask;
    float localMean;
    float localSD;
    float localMedian;
};
static struct subBlock subBlockList[4];
#define BLOCK_ROW 224 //sub-block row
#define BLOCK_COL 224 //sub-block col
#define imageSize 448

结构的每个位置都占总图像的四分之一,如下所示:

subBlockList[0].vertMask 包含一个 unsigned char* 2d(大小为 224x224 的数组)NW vals

subBlockList[1].vertMask 包含一个 unsigned char* 2d(大小为 224x224 的数组)NE vals

subBlockList[2].vertMask 包含一个 unsigned char* 2d(大小为 224x224 的数组)SW vals

subBlockList[3].vertMask 包含一个 unsigned char* 2d(大小为 224x224 的数组)SE Vals

将 4 个部分放回一个 image_ptr 的函数(THE ONE THROWING ERRORS)

image_ptr buildImage(){
   image_ptr retVal; // contains the image pointer to return
   unsigned char subImageTempHolder[imageSize][imageSize];
   int subBlockSize = 224;
        //NW Corner
        for (int i=0; i< subBlockSize; i++) { // 0<224
            for (int j=0; j< subBlockSize; j++){ // 0<224
                   subImageTempHolder[i][j] = subBlockList[0].vertMask[i][j];
            }
        }
        //NE Corner
        for (int i=0; i< subBlockSize; i++) { //0 <224
            for (int j=subBlockSize; j< imageSize; j++){ //224 < 448
                subImageTempHolder[i][j] = subBlockList[1].vertMask[i][j];
            }
        }
        //SW Corner
        for (int i=subBlockSize; i< imageSize; i++) { //224 <448
            for (int j=0; j< subBlockSize; j++){ //0 < 224
                subImageTempHolder[i][j] = subBlockList[2].vertMask[i][j];
            }
        }
        //SE Corner
        for (int i=subBlockSize; i< imageSize; i++) { //224 < 448
            for (int j=subBlockSize; j< imageSize; j++){ //224 <448
                subImageTempHolder[i][j] = subBlockList[3].vertMask[i][j];
            }
        }
        retVal = (image_ptr) subImageTempHolder;
        return retVal;
}

它是这样设置的:

subBlockList[blockPos].vertMask = verticalMask(block); 
//I didnt include the function using line above ^ but you should get the idea.


unsigned char* verticalMask(unsigned char paramBlock[BLOCK_ROW][BLOCK_COL]) {
    unsigned char retVal[BLOCK_ROW][BLOCK_COL]; //return value
    double pixelVal;
    double min = DBL_MAX;
    double max = -DBL_MAX;
    //3x3 Gy Sobel Mask
    int Gy[3][3];
    Gy[0][0] = 1; Gy[0][1] = 2; Gy[0][2] = 1;
    Gy[1][0] = 0; Gy[1][1] = 0; Gy[1][2] = 0;
    Gy[2][0] = -1; Gy[2][1] = -2; Gy[2][2] = -1;

    //filtering
    for (int y = 0; y<= BLOCK_COL-1; y++) {
        for (int x=0; x <= BLOCK_ROW-1; x++) {
            pixelVal = 0.0;
            for (int i = -1; i <= 1; i++) {
                for (int j = -1; j <= 1; j++) {
                    pixelVal += Gy[i+1][j+1] * paramBlock[y+i][x+j];
                }
            }
            if (pixelVal < min) {
                min = pixelVal;
            }
            if (pixelVal > min) {
                max = pixelVal;
            }
        }
    }
    if((int)(max - min) == 0) {
        printf("Error nothing exists");
    }

    //generate image
    for (int y = 1; y < BLOCK_COL - 1; y++) {
        for (int x = 1; x < BLOCK_ROW - 1; x++) {
            pixelVal = 0.0;
            for (int j = -1; j <= 1; j++) {
                for (int i = -1; i <= 1; i++) {
                    pixelVal += Gy[j + 1][i + 1] * paramBlock[y + j][x + i];
                }
            }
            pixelVal = max * (pixelVal - min) / (max - min); //MAX_BRIGHTNESS
            retVal[y][x] = (unsigned char)pixelVal;
            }
        }
    return retVal;
}

【问题讨论】:

  • 如果vertMaskchar*,那么只有vertMask[i] 才有意义。一个* 与一组[...] 配对。你有两个。你的意思是vertMask[i + j * imageSize]
  • 你对`image_ptr`的定义是什么?
  • image_ptr 在另一个文件中定义为“typedef unsigned char *image_ptr;”

标签: arrays c pointers


【解决方案1】:

这是subBlockList[0].vertMask[i][j] 中的vertMask[i][j]

subBlockList[0] 返回一个struct subBlock.vertMask 是一个无符号字符数组。 .vertMask[i] 返回 .vertMask 中的第 i 个 unsigned char.vertMask[i][j] 要求 unsigned char 的第 j 个元素,这没有意义。

【讨论】:

  • 好的,我会将结构更改为无符号字符“(星...星)”vertMask;以及将其返回到 unsigned char"(star...star)" vertMask(unsigned char paramBock[size][size]. 以便它能够容纳二维数组的函数?感谢您的帮助
  • 修复了我的编译错误,我将在指针方面做更多阅读,感谢帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多