值存储在结构中

发布于 2024-12-10 19:15:45 字数 712 浏览 0 评论 0 原文

我是C语言新手。我有一些关于结构的问题要问。

例如:

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;

*对于struct mmc_davinci_host,请参阅此站点http://lxr.free-electrons.com/source/drivers/mmc/host/davinci_mmc.c#L167*

函数mmc_priv() 返回一个空指针。那么,由于 host 是结构体类型,所以 host 将返回的地址存储在哪里呢?

谢谢。

i'm new to C language. 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*

The function mmc_priv() returns a void pointer. So, where does host store the returned address since host is a struct type?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪冰清 2024-12-17 19:15:45

返回void*并不意味着返回void指针。它意味着返回指向任何类型的指针
事实上,指针存储的是地址,并且地址总是具有相同的大小,无论后面是哪种类型。

但是,在调用 mmc_priv 后,您应该强制转换为 struct mmc_davinci_host *。我会写如下:

/* 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);

Returning void* does not mean to return a void pointer. 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. 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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文