user10014964
user10014964

Reputation:

why is the difference in address of character and int in a structure in c =4 and not 1

I gave the following input in the below code
a 12.5 12
b 13.5 13
c 14.5 14

#include <stdio.h>
int main(){

struct book
{
char name;
float price;
int pages;
};

struct book b1,b2,b3;



printf("enter name , price ,& no of pages\n");
scanf(" %c %f %d",&b1.name,&b1.price,&b1.pages);



scanf(" %c %f %d",&b2.name,&b2.price,&b2.pages);


scanf(" %c %f %d",&b3.name,&b3.price,&b3.pages);



printf("%c %d %f %d %d %d",b1.name,&b1.name,b1.price,&b1.price,b1.pages,&b1.pages);

printf("\n%c %d %f %d %d %d",b2.name,&b2.name,b2.price,&b2.price,b2.pages,&b2.pages);

printf("\n%c %d %f %d %d %d",b3.name,&b3.name,b3.price,&b3.price,b3.pages,&b3.pages);

printf("\n%d %d %d",sizeof(b1.name),sizeof(b1.price), sizeof(b1.pages));

}

And I got the following output

a 2686740 12.500000 2686744 12 2686748
b 2686728 13.500000 2686732 13 2686736
c 2686716 14.500000 2686720 14 2686724
1 4 4

Now what I can't understand is that the difference between the address of b1.name(2686740) and b1.price(2686744) is 4 wheres b1.name is a character so the difference should have been 1 and the same is repeated in b2 and b3 also. Please help!!!

Upvotes: 1

Views: 113

Answers (2)

CIsForCookies
CIsForCookies

Reputation: 12837

Having a struct with different types could be packed tightly (which means the difference between addresses will be as you thought):

|--0--|--1--|--2--|--3--||||--4--|--5--|--6--|--7--||||--8--|--9--|--10--|--11--|
   c     f0    f1    f2       f3    i0    i1    i2    i3

This can be achieved by using #pragma pack(1)

#pragma pack instructs the compiler to pack structure members with particular alignment

But, since you didn't order the compiler to pack in 1-byte chunks, it is packing in n-byte chunks, where n = size of largest type inside struct, which in your case is 4-byte, resulting in:

|--0--|--1--|--2--|--3--||||--4--|--5--|--6--|--7--||||--8--|--9--|--10--|--11--|
   c    pad0  pad1  pad2      f0    f1    f2       f3    i0    i1    i2     i3

Play a bit with this snippet:

struct A{
    char c;   // has 7 bytes padding due to having a double in the struct
    double d; // size 8
}AA;

    printf("%d\n", sizeof(AA));

// feel free to change the parameter inside the pragma pack
#pragma pack(1) 
struct B{
    char c;   // has no padding due to pack(1)
    double d;
}BB;
#pragma pack(0)

    printf("%d\n", sizeof(BB));

#pragma pack(2)
struct C{
    char c;    // has only 1 byte padding
    double d;
}CC;
#pragma pack(0)

    printf("%d\n", sizeof(CC));

Upvotes: 0

kiran Biradar
kiran Biradar

Reputation: 12742

It is because of structure padding.

Architecture of a computer processor is such a way that it can read 1 word from memory at a time.

To make use of this advantage of processor, data are always aligned as 4 bytes package which leads to insert empty addresses between other member’s address. e.g on 64 bit system,int should start at address divisible by 4, and long by 8, short by 2.

Padding is on by default. It inserts the following gaps into your first structure:

struct book
{
   char name;
   char padded_gap[3]; //padding extra 3 bytes
   float price;
   int pages;
};

Upvotes: 3

Related Questions