简体   繁体   English

我如何返回指向一维结构 function 的指针,其中包含各种结构?

[英]How do i return a pointer to a 1 dimension struct function with a various structs inside?

I have this function which has to return a pointer to a 1-dimension array of various (65,535 for this picture) structs.我有这个 function ,它必须返回一个指向各种(这张图片为 65,535)结构的一维数组的指针。 But it returns only the first element of this array, and I am curious what the problem is.但它只返回这个数组的第一个元素,我很好奇问题出在哪里。

struct pixel* make_pixel (int red, int green, int blue){

    struct pixel* pix = malloc(sizeof(struct pixel));

    pix->blue = blue;
    pix->red = red;
    pix->green = green;

    return (pix);
}

struct pixel* read_data(FILE *stream, const struct bmp_header *header) {
    
    if(stream == NULL || header == NULL) return 0;

    struct pixel *pix = malloc(sizeof(struct pixel));

    struct pixel *data[header->width * header->height]; //would be great to do this dynamically

    for (int i = 0; i < (header->width * header->height); i++) {
        fread(pix, sizeof(struct pixel), 1, stream); //reads pixels 1 by 1 from stream
        data[i] = make_pixel(pix->red, pix->green, pix->blue); //put pixel as an i'th element of 'data' array
    }

    free(pix);

    return (*data);
}

int main() {

    const char* fileName = "C:/TheWaaay.../assets/lenna.bmp";
    FILE* image = fopen(fileName, "rb");

    struct bmp_header *header =  read_bmp_header(image);

    struct pixel *data = read_data(image, header);

    return 1;
}

The structure of actual pixel:实际像素的结构:

struct pixel {
    uint8_t blue;
    uint8_t green;
    uint8_t red;
    //uint8_t alpha;
} __attribute__((__packed__));

You should make data be a pointer to pointers, and return that.您应该使data成为指向指针的指针,并返回它。

struct pixel **read_data(FILE *stream, const struct bmp_header *header) {

    if(stream == NULL || header == NULL) return 0;

    struct pixel *pix;
    struct pixel **data; 

    pix = malloc(sizeof *pix);
    data = malloc(header->width * header->height * sizeof *data);

    for (int i = 0; i < header->width * header->height; i++) {
        fread(pix, sizeof(struct pixel), 1, stream); 
        data[i] = make_pixel(pix->red, pix->green, pix->blue); 
    }

    free(pix);

    return data;
}

main would become: main将变为:

int main() {

    const char* fileName = "C:/TheWaaay.../assets/lenna.bmp";
    FILE* image = fopen(fileName, "rb");

    struct bmp_header *header =  read_bmp_header(image);

    struct pixel **data = read_data(image, header);

    for(int i = 0; i < header->width * header->height; i++) {
        // do something here with data[i]
        struct pixel *pix = data[i];
        // manipulate pix, which points to data[i]
    }

    return 1;
}

data is an array of pointers, not an array of pixels. data是指针数组,而不是像素数组。 You can allocate an array of pixels dynamically, and read directly into that.您可以动态分配像素数组,然后直接读取。

struct pixel* read_data(FILE *stream, const struct bmp_header *header) {

    if(stream == NULL || header == NULL) return 0;

    struct pixel *data = malloc(header->width * header->height * sizeof(struct pixel));
    if (data == NULL) {
        return NULL;
    }
    fread(&pixel[i], sizeof(struct pixel), header->width * header->height, stream);

    return data;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM