C passed as char *?

I have a little problem in a scenario where:

while (read(dp->fd, (char *) &dirbuf, sizeof(dirbuf)) == sizeof(dirbuf)) { ... }

where dirbuf:

struct direct dirbuf { 
    ino_t d_ino; char d_name[DIRSIZ]; 
};  

How does C know to read data in our structure? With read () How, the alignment of our dirbuf fits perfectly into an array of characters? since read accepts void * for its second argument, passing the structure as cast to char *, it makes very little sense to me, how does it fit other member elements correctly?

I am not sure if I am asking this question correctly, I hope someone can decipher my problem behind my madness and help me.

Thank.

+5
source share
3 answers

Hysterical Raisens

char * - , . , - , , .

- void, API, read(2), char *, char *.

read(2) void * , . , - .

, , .

.

, . , , , .

+7

. ( sizeof) ( &dirbuf) .

, , , , .

(char *) . (void *) . , void * .

+4

C langauage, void * char *. , void *, - char * ( - void *)

read(dp->fd, &dirbuf, sizeof(dirbuf)) 

C , . - , , dirbuf. (, ), , .

Please note that if you are not working with something that is covered by a very strict specification, such split-write binary read / write methods can be used only during one session of the same program or, possibly, between different sessions of the same the same version of the program on the same platform. After you start working with different platforms and / or different versions of code on the same platform, a thing can easily fall apart after changing the format of the data memory.

+1
source

All Articles