简体   繁体   English

值存储在结构中

[英]values store in struct

i'm new to C language. 我是C语言的新手。 I have some questions to ask regarding structs. 关于结构,我有一些问题要问。

For example: 例如:

static inline void *mmc_priv(struct mmc_host *host)
{        
     return (void *)host->private;
}

struct mmc_host 
{
    unsigned long private[0] ____cacheline_aligned;
};


struct mmc_davinci_host *host = NULL;
struct mmc_host *mmc = NULL;

host = mmc_priv(mmc);
host->mmc = mmc;

*for the struct mmc_davinci_host please refer to this site http://lxr.free-electrons.com/source/drivers/mmc/host/davinci_mmc.c#L167 * *有关struct mmc_davinci_host请参考此网站http://lxr.free-electrons.com/source/drivers/mmc/host/davinci_mmc.c#L167 *

The function mmc_priv() returns a void pointer. 函数mmc_priv()返回空指针。 So, where does host store the returned address since host is a struct type? 那么,哪里host ,因为存储返回地址host是一个结构类型?

Thank you. 谢谢。

Returning void* does not mean to return a void pointer. 返回void*并不意味着返回void指针。 It means to return a pointer to any type . 这意味着返回一个指向任何类型的指针
In fact, pointers store addresses, and addresses always have the same size, no matter which type is located behind. 实际上,指针存储地址,并且无论后面是哪种类型,地址始终具有相同的大小。

However, you should have a cast to struct mmc_davinci_host * after calling mmc_priv . 但是,在调用mmc_priv之后,应该struct mmc_davinci_host *struct mmc_davinci_host * I would write it as follows: 我将其编写如下:

/* call mmc_priv and store its result in host, after having cast it to struct mmc_davinci_host * */
host = (struct mmc_davinci_host *) mmc_priv(mmc);

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

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