将值设置为2d动态数组C



我必须确保行和列有效,并且我必须将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 == NULLn");
return 1; //exit with a non-zero value
}
printf("test_im created successfully.nn");

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 %dn", 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, unsigned int row, unsigned int col, 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

那么我想知道,当您实际想要存储uint8_t时,为什么您的set函数会将值取为int。这并不违法,但有点奇怪。

我得出了与kaylum相同的结论:

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

通过阅读您的代码,我解释了您的API概念,因此解决方案只是简单地更改

return val;

return IMG_OK;

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

相关内容

  • 没有找到相关文章

最新更新