【问题标题】:Are there placeholders for attributes of a struct? (C) [closed]结构的属性是否有占位符? (C) [关闭]
【发布时间】:2021-11-13 05:45:34
【问题描述】:

我正在尝试使用 CS50 的滤镜,但我正处于像素模糊部分。我可以使用

访问像素的颜色
image[row][pixel].rgbtRed
image[row][pixel].rgbtGreen
image[row][pixel].rgbtBlue

我希望能够调用一个函数来计算周围像素的平均值并传入我想要平均值的颜色。是否有某种占位符以便我可以访问结构的特定元素/属性? (不确定它的正确名称抱歉)。

我还是个新手,试着把它放在括号里等等,但没有任何效果。

这是我尝试获取传递颜色值的函数。

float calcAverage(int height, int width, RGBTRIPLE image[height][width], int row, int pixel, string color)
{
    float sum =  image[row][pixel].color + image[row][pixel - 1].color; 
    return 0;
}

这就是我调用函数的方式。

redAverage = calcAverage(height, width, image, row, pixel, "rgbtRed");

现在我的 .color 计划不起作用,因为我知道他正在寻找一个名为 color 的属性。这是我得到的错误

error: no member named 'color' in 'RGBTRIPLE'
float sum =  image[row][pixel].color + image[row][pixel - 1].color;

出于测试目的,我将总和保持简短。在此先感谢,我开始认为这是不可能的,我应该离开它。如果我使用了错误的术语来表示我正在寻找的内容,再次抱歉。

【问题讨论】:

  • 顺便说一句,你不想让你的函数返回sum而不是0,还是做更多的计算(除以2)?就目前而言,该功能没有完成任何事情。
  • @NateEldredge OP 想要做相当于 eval 或宏的操作。
  • 哦,现在我明白了 - 我错过了 color 是函数的参数。对,在 C 中没有反射。我想有 offsetof 但我也不打算向初学者推荐它。
  • @NateEldredge 哦,是的,当然,我只是将它设置为返回 0 用于测试目的,因为我想通过先编译来完成哈哈
  • 由于 C 没有任何类型的 introspection 这是不可能的。您必须自己解析字符串,然后从相应的成员中获取值。

标签: c struct cs50 placeholder


【解决方案1】:

这可以通过成员的偏移量来完成,如下面的代码所示。从某种意义上说,使用它有点笨拙。但是,它在某些情况下可能有用且合适。因为使用了显式转换,覆盖了关于类型的常见编译器警告,所以必须小心。

typedef struct { float red, green, blue; } RGBTRIPLE;


#include <stdio.h>
#include <stddef.h>


/*  Get the offset of a member in an RGBTRIPLE using the C standard "offsetof"
    feature.  This could be a function, preferably a static inline function
    visible where it is used.
*/
#define OffsetOf(member)    (offsetof(RGBTRIPLE, member))

/*  Get a member of an RGBTRIPLE by its offset.

    This uses the offset to locate the member and then converts the address to
    a pointer to the member type, which we then use with "*" to refer to the
    member.  That produces an lvalue for the member which can be used to read
    (use the value of) or write (assign to) the member.

    This must be a macro because a function cannot return an lvalue.
*/
#define MemberByOffset(Structure, Offset) \
    (*(float *)((char *) &Structure + Offset))


static float Average(RGBTRIPLE *Array, size_t Offset)
{
    static const size_t N = 2;  // For demonstration only.

    float sum = 0;
    for (size_t i = 0; i < N; ++i)
        sum += MemberByOffset(Array[i], Offset);

    return sum/N;
}


int main(void)
{
    RGBTRIPLE Array[] = {{ 10, 20, 30 }, { 100, 200, 300 }};
    printf("Red average = %g.\n",   Average(Array, OffsetOf(red  )));
    printf("Green average = %g.\n", Average(Array, OffsetOf(green)));
    printf("Blue average = %g.\n",  Average(Array, OffsetOf(blue )));
}

【讨论】:

    【解决方案2】:

    您不能使用字符串变量代替成员名称。您需要检查字符串的值并根据该值选择字段。

    float calcAverage(int height, int width, RGBTRIPLE image[height][width], 
                      int row, int pixel, string color)
    {
        if (!strcmp(color, "rgbtRed")) {
            return image[row][pixel].rgbtRed + image[row][pixel - 1].rgbtRed;
        } else if (!strcmp(color, "rgbtGreen")) {
            return image[row][pixel].rgbtGreen + image[row][pixel - 1].rgbtGreen;
        } else if (!strcmp(color, "rgbtBlue")) {
            return image[row][pixel].rgbtBlue + image[row][pixel - 1].rgbtBlue;
        } else {
            // invalid color
            return 0;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-12-27
      • 2011-07-12
      • 1970-01-01
      • 1970-01-01
      • 2014-12-20
      • 2017-03-21
      • 1970-01-01
      • 1970-01-01
      • 2015-03-27
      相关资源
      最近更新 更多