gpt4 book ai didi

c - ‘->’ 的无效类型参数(有 ‘color’ )

转载 作者:太空狗 更新时间:2023-10-29 16:01:32 25 4
gpt4 key购买 nike

我有以下源代码,它为图像及其像素分配空间并读取像素值。

#include <stdlib.h>
#include <stdio.h>



typedef struct color
{
int r,g,b;
}color;


typedef struct image
{
int width, height;
color *pixels;
}image;

image* CreateImage(int width, int height)
{

imagem *im=NULL;
im=(image*)malloc(sizeof(image));
im->height=height;
im->width=width;
im->pixels=(color*)malloc(width*height*sizeof(color));

int i;

//error starts here
for (i=0; i<width*height;i++)
{
scanf('%d', &im->pixels[i]->r);
scanf('%d', &im->pixels[i]->g);
scanf('%d', &im->pixels[i]->b);

}


return im;
}

问题始于读取图像像素的代码部分。当我编译它时,错误是''->'(有'color')的无效类型参数'

我知道如果左操作数是指针,我们必须使用'->'。这里图像和像素是指针,那么为什么我不能使用 im->pixels[i]->r,例如?我该如何解决这个问题?

最佳答案

pixels 确实是一个指针,但是您对 [] 的使用已经取消了对它的引用。你想要:

 &im->pixels[i].r

请注意,您的 scanf 调用应该将字符串作为第一个参数,而不是多字 rune 字 - 在此处使用双引号。

关于c - ‘->’ 的无效类型参数(有 ‘color’ ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27510225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com