加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

‘ – >’的无效类型参数(有’颜色’)

发布时间:2020-12-16 09:55:38 所属栏目:百科 来源:网络整理
导读:我有以下源代码,它为图像,像素和读取像素值分配空间. #include stdlib.h#include stdio.htypedef struct color{ int r,g,b;}color;typedef struct image{ int width,height; color *pixels;}image;image* CreateImage(int width,int height){ imagem *im=NUL
我有以下源代码,它为图像,像素和读取像素值分配空间.

#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;
}

问题始于代码中读取图像像素的部分.当我编译它时,错误是’无效类型参数’ – >'(有’颜色’)’

我知道我们必须使用’ – >’如果左操作数是指针.这里的图像和像素是指针,所以为什么我不能使用im-> pixels [i] – > r?我怎么解决这个问题?

解决方法

scanf("%d",&im->pixels[i].r);

pixels[i] = *(pixels + i); /* [] has already dereferenced */

如上所示,您需要在scanf()中使用双引号而不是单引号

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读