struct - c structure problem -


thanks support solving previous problems. i'm studying self referential structures. have written following code:

#include <stdio.h>  int main() { system("clear"); struct node {  int x;   struct node *next; } p1;  printf(" \nthe address of node1 = %u",& p1);  printf(" \n\nthe size of node 1 = %d",sizeof( p1)); printf("\n\n size of info part = %d",sizeof(p1.x)); printf("\n\n size of pointer part = %ld",sizeof(p1.next)); printf("\nthe size of node = %d\n",sizeof(struct node)); return; } 

the program compiled few warning like:

warning: format ‘%u’ expects type ‘unsigned int’, argument 2 has type ‘struct node *’

every time pointer such warning generated. problem? don't know that. can explain why happen on linux (specially)?

my second question run program shows size of structure 16 while int take 4 byte (ubuntu 10) & pointer of 8 byte. why shows size of structure 16 byte?

in c99, use '%zd' print size_t.

elsewhere, cast result of sizeof() int; aren't going overflow anything.

to print pointers in c99, if don't default format %p:

#include <inttypes.h>  printf("pointer = 0x%" prixptr "\n", (uintptr_t)&something); 

the type uintptr_t guaranteed big enough hold pointers.

and size issue because of alignment requirements; 8-byte pointers must 8-byte aligned optimal performance (on machines, avoid crashes). so, structure must multiple of 8 bytes long, , 16 smallest multiple of 8 larger 12 bytes. you'll have padding between 2 parts of structure - 4 bytes of padding, in fact. use offsetof() macro <stddef.h> demonstrate that.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -

PostgreSQL 9.x - pg_read_binary_file & inserting files into bytea -