question

$$ANON_USER$$ avatar image
0 Votes"
$$ANON_USER$$ asked $$ANON_USER$$ commented

The length of character is showing four bytes, instead of one.

We know that the elements of a structure are stored in contiguous memory location, but when i declared the dat structure type and variable and when i printed i got the address showing four bytes, so please help me out. This is the code---- #include<stdio.h> #include<stdlib.h> int main() { struct book { char name; float price; int pages; }; struct book b1 = { 'B',130.00,550 }; printf("Address of name=%u\n", &b1.name); printf("Address of name=%u\n", &b1.price); printf("Address of name=%u\n", &b1.pages); return 0; }![94896-char-byte.png][1] [1]: /answers/storage/attachments/94896-char-byte.png

c++
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

And after you correct your misunderstanding about how the structure is arranged, you might also want to learn the correct method of printing pointer values in printf. Look at the %p conversion specification in your printf reference.

1 Vote 1 ·
DavidLowndes-6766 avatar image
0 Votes"
DavidLowndes-6766 answered

Have a read about structure alignment and packing.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RLWA32-6355 avatar image
0 Votes"
RLWA32-6355 answered

I think you are misinterpreting the output of the test program. It prints the address of various structure members, not their length. It seems you are interpreting the difference between the addresses as the length of a member. This would be incorrect.

After reading about structure alignment and packing you should also read about sizeof-operator-c. When a structure contains padding its size will not be the sum of the sizes of its members.

So take a look at applying sizeof to the book structure and then to its individual members.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

WayneAKing-0228 avatar image
0 Votes"
WayneAKing-0228 answered $$ANON_USER$$ commented

By way of an example of some of the points made
by others, consider the following example. It
defines the packing for the book struct as being
1 byte, but retains the default packing for any
other structs.

 #include<stdio.h>
 #include<stdlib.h>
 int main()
 {
 #pragma pack(push,1)
     struct book
     {
         char name;
         float price;
         int pages;
     };
 #pragma pack(pop)
    
     struct book2
     {
         char name;
         float price;
         int pages;
     };
    
     struct book b1 = { 'B',130.00,550 };
     printf("Address of name=%u\n", &b1.name);
     printf("Address of name=%u\n", &b1.price);
     printf("Address of name=%u\n", &b1.pages);
    
     printf("Address of name=%p\n", &b1.name);
     printf("Address of name=%p\n", &b1.price);
     printf("Address of name=%p\n", &b1.pages);
    
     printf("%u\n", sizeof b1);    
     printf("%u\n", sizeof (struct book));
        
     struct book2 b2;
     printf("%u\n", sizeof b2);    
     printf("%u\n", sizeof (struct book2));
    
     return 0;
 }

Sample output:

Address of name=4061036
Address of name=4061037
Address of name=4061041
Address of name=003DF76C
Address of name=003DF76D
Address of name=003DF771
9
9
12
12

  • Wayne

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for the information...

0 Votes 0 ·