0

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.

jopasserat
  • 5,721
  • 4
  • 31
  • 50
space boy
  • 3
  • 1

1 Answers1

1

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);
jopasserat
  • 5,721
  • 4
  • 31
  • 50
  • Yes.I do understand the `void*` is to casting to any type.But since struct has its own declared members.Where are the address store?Is it at the 1st byte of `struct`?? – space boy Oct 20 '11 at 07:45
  • `host` is a pointer, not directly a `struct`. It points to a `struct` where the members are located. – jopasserat Oct 20 '11 at 07:49
  • okkk.I understand it already. i miss that host is a pointer.Thamk for pointing it out.Do you have any good e-book/site for learning advance C? – space boy Oct 20 '11 at 07:53
  • 1
    @spaceboy: For ebook list refer [this link on SO](http://stackoverflow.com/questions/194812/list-of-freely-available-programming-books) and [this link on SO](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) for C programming books – another.anon.coward Oct 20 '11 at 08:01
  • The conversion from `void*` can be done implicitly; the cast is unnecessary. – Keith Thompson Oct 20 '11 at 09:03
  • @spaceboy: look at the eBooks link that another.anon.coward pointed out – jopasserat Oct 20 '11 at 09:20
  • @Keith Thompson: of course it's implicit. However, verbosity is always better in software engineering. The clearer the code, the better. – jopasserat Oct 20 '11 at 09:21
  • @jHackTheRipper: Not always. For example, a cast can mask a failure to include a declaration for `mmc_priv`, which (prior to C99) causes the compiler to assume that it returns `int`. C has implicit conversions for a reason. – Keith Thompson Oct 20 '11 at 09:38
  • @Keith Thomspon: that is why is not the safest language ever! But I would encourage every programmer to add these explicit casts, coupled with a comment so that in case of failure, it could be a hint to debug. – jopasserat Oct 20 '11 at 09:47
  • @jHackTheRipper: The casts add *nothing* to the program's functionality or safety. – Keith Thompson Oct 20 '11 at 17:32