简体   繁体   English

访问通过引用接收的结构的成员

[英]Accessing members of a struct that is received by reference

Hi I have a few doubts about how to access the members of a struct, in case that the struct is recived by reference by some function. 嗨,我对如何访问某个结构的成员有一些疑问,以防该结构被某些函数引用引用。

here is a program that generates a random "image" and have 2 examples of my problems: 这是一个生成随机“图像”并有2个我的问题示例的程序:

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

#define WIDTH 16
#define HEIGHT 8

typedef struct
{
    int width;   
    int height; 
    int maxvalue;
    char *pixels;
} image;


void setimagemaxvalue (image *img)
{

    scanf ("%d", &((*img).maxvalue); //I know is wrong 

}

void createimage (image *img) //just creates a random matrix of pixels
{
        (*img).pixels=malloc((img->width)*(img->height));

        int i,j;

    char *tmp;
    tmp=(*img).pixels;

    for (i=0;i<(img->height);i++)
        {
        for (j=0;j<(img->width);j++)
        {
            *(tmp+(i*(img->width)+j))=(char)(rand()%(img->maxvalue));
        }

    }   

}



int main ()
{
    srand(time(NULL));
    image img;

    img.width=WIDTH;
    img.height=HEIGHT;

    setimagemaxvalue(&img);
    createimage (&img);

    return 0;
}

My questions are: 我的问题是:

1) What should be the parameter send to the scanf function? 1)应该发送给scanf函数的参数是什么? (forget about the buffer cleaning issue, and yes, I know that I can just send the maxvalue member of the struct to the "setimagemaxvalue" function, but this is just an example, I want to know how to do it considering that it's a member of a struct. (忘了缓冲区清理问题,是的,我知道我可以将结构的maxvalue成员发送到“ setimagemaxvalue”函数,但这只是一个例子,我想知道如何做到这一点,因为这是一个结构的成员。

2) How could I write the function "createimage" without using the tmp variable (there is the pointer img referencing the pointer pixels and it's referencing the data). 2)我如何不使用tmp变量编写函数“ createimage”(存在指向指针像素的img指针,并且它正在引用数据)。

1) About the parameter of scanf 1)关于scanf的参数

scanf ("%d", &((*img).maxvalue));

is right way to read the value. 是读取值的正确方法。 Alternatively you can also use 另外,您也可以使用

scanf ("%d", &(img->maxvalue));

Similarly you can replace (*img).pixels with img->pixels if you think later is more readable. 同样,如果您认为以后更易读,可以将(*img).pixelsimg->pixels

2) You can replace all occurances of tmp with img->pixels 2)您可以用img->pixels替换tmp所有出现

You start by writing: 'received by reference.' 您首先写:“被参考接受”。 , but you are actually passing you struct by pointer. ,但实际上是通过指针传递struct。 Others have showed that you can use -> which is the correct way to address members when using pointer. 其他人表明,可以使用->这是使用指针寻址成员的正确方法。

But I think in your case (looking at main) you would be better of passing by reference instead. 但是我认为在您的情况下(从主要角度看),您最好通过引用传递。 eg 例如

void setimagemaxvalue (image &img)
{
    scanf ("%d", &(img.maxvalue);
}

that way you can use . 这样就可以使用了。 instead of -> and in your main it would look like this: 而不是->并且在您的主目录中看起来像这样:

image img;
....

setimagemaxvalue( img );

No ampersand in front of img img前面没有“&”号

Best regards Asger-P 最好的问候Asger-P

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

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