微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将值设置为二维动态数组数组 C

如何解决将值设置为二维动态数组数组 C

我必须确保 row 和 col 有效,并且我必须将 im->pixels[row][col] 的值设置为 val 并返回 IMG_OK
否则,数组将不被修改并返回。我知道问题出在 img_result_t img_set() 上,但我无法弄清楚。
我无法将 val 设置为我的数组。在运行 main() 时,我得到的输出是,

        Creating test_im by calling 'img_create(10,10)'
        test_im created successfully.
    
        Testing img_set.
        Cannot set value at index 0

代码

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

typedef struct { 
    uint8_t** pixels;
    unsigned int rows;
    unsigned int cols;
} img_t;

/// A type for returning status codes 

typedef enum {
    IMG_OK,IMG_BADINPUT,IMG_BADARRAY,IMG_BADCOL,IMG_BADROW,IMG_NOTFOUND
} img_result_t;


img_t *img_create(unsigned int rows,unsigned int cols){

    img_t* arr = malloc(sizeof(img_t));
  if(arr == NULL) return arr;

  arr->rows = rows;
  arr->cols = cols;

  arr->pixels = malloc(rows * sizeof(*arr->pixels));
  if(arr->pixels == NULL){
    free(arr);
    return NULL;
  }
  for(unsigned int i = 0; i<arr->rows; i++){
        arr->pixels[i] = malloc(cols*sizeof(img_t));
        if(arr->pixels[i] == NULL){
          for(int j= 0; j < i; j++){
            free(arr->pixels[i]);
          }
          free(arr->pixels);
          free(arr);
          return NULL;
          
        }
      }return arr;
}

void img_destroy(img_t* im){

    if(im != NULL){
      for(unsigned int i = 0; i < im->rows; i++){
        free(im->pixels[i]);
      }
        free(im->pixels);
        free(im);
    }
}


img_result_t img_set(img_t* im,unsigned int row,unsigned int col,int val){
  
  if(im == NULL) return IMG_BADARRAY;

  im->rows = row;
  im->cols = col;
  
  unsigned int empty = 0;

  if(row <= empty){
    return IMG_BADROW;
  }
  if(col <= empty){
    return IMG_BADCOL;
  }
  im->pixels[row][col] = val;
  return val;

}

// helper function that prints the content of the img
void print_img(img_t* im) {
    if (im == NULL) {
        printf("Invalid img (null).\n");
        return;
    }

    printf("Printing img of row length %d and col length %d:\n",im->rows,im->cols);
    for (unsigned int i=0; i<im->rows; i++) {
        for (unsigned int j=0; j<im->cols; j++) {
            printf("%d ",im->pixels[i][j]);
        }
        printf("\n");
    }
    printf("\n");
}

int main() {
    // test variables to hold values returned by the functions
    img_t* test_im = NULL;
    img_t* null_im = NULL;
    img_t* test2_im = NULL;
    img_result_t test_result = IMG_OK;
    int val;
    
    printf("Creating test_im by calling 'img_create(10,10)'\n");
    test_im = img_create(10,10);
    if (test_im == NULL) {
        printf("test_im == NULL\n");
        return 1; //exit with a non-zero value
    }
    printf("test_im created successfully.\n\n");

    

    printf("Testing img_set.\n");
    for (unsigned int i=0; i<test_im->rows; i++) {
        for (unsigned int j=0; j<test_im->cols; j++) {
            if (img_set(test_im,i,j,(rand()%100)) != IMG_OK) {
                printf("Cannot set value at index %d\n",i);
                return 1; //exit with a non-zero value
            }
        }
    }
}

解决方法

您的 img_set 函数有 4 个错误。

  1. 您覆盖了行和列的 img_t 配置

  2. 您对下边界的检查有误

  3. 不检查上限

  4. 存储值时返回类型错误。

查看评论。

img_result_t img_set(img_t* im,unsigned int row,unsigned int col,int val){
  
  if(im == NULL) return IMG_BADARRAY;

  im->rows = row;  <--- Changing im->rows and cols are wrong.
  im->cols = col;  <--- Once im is created you never want to change them
  
  unsigned int empty = 0;  <--- why ? zero is always lower boundary

  if(row <= empty){ <--- This makes index zero invalid
    return IMG_BADROW;
  }
  if(col <= empty){ <--- This makes index zero invalid
    return IMG_BADCOL;
  }

  // Here should check for upper boundary

  im->pixels[row][col] = val;
  return val; <-------- Wrong return type

}

试试:

img_result_t img_set(img_t* im,int val)
{
  if(im == NULL) return IMG_BADARRAY;

  if(row < 0 || row >= im->rows) return IMG_BADROW;

  if(col < 0 || col >= im->cols) return IMG_BADCOL;

  im->pixels[row][col] = val;
  return IMG_OK;
}

此外,您在创建函数中还有一个错误:

arr->pixels[i] = malloc(cols*sizeof(img_t));
                                    ^^^^^
                                    wrong type

然后我想知道为什么当您真正想要存储 int 时,您的 set-function 将值设为 uint8_t。这不违法,但有点奇怪。

,

我得出了与凯勒姆相同的结论:

该函数在您检查 return val; 时执行 img_set(test_im,i,j,(rand()%100)) != IMG_OK

通读你的代码,我会解释你的 API 概念,所以解决方案是简单地改变

return val;

return IMG_OK;

因为这是成功情况下 img_result_t 的预期状态返回值。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。