struct ptr
{
int a;
char b;
int *p;
}abc;
what is d sizeof structure without using "sizeof" operator??

Answers were Sorted based on User's Feedback



struct ptr { int a; char b; int *p; }abc; what is d sizeof structure without using "sizeo..

Answer / vadivel t

Hi All,
The size of any data type is depends on the compiler
(including struct, union and enum). But the question does
not mean, "what is the size of the given structure".

It actually means,
Find the size of the structure without using sizeof()
operator.

The Answer, irrespective of compiler would be,

Output of the following code.

-First printf gives the size of the structure, wthout using
size of operator.

-U can cross check the ans using sizeof() operator in the
second printf().

#include<stdio.h>
struct name
{
int a;
char b;
int *p;
}abc;

main()
{
struct name *ptr, *ptr1;
ptr = &abc;
ptr1 = ptr + 1;
printf("WITHOUT USING sizeof() OPERATOR: %d \n",((char *)
ptr1 - (char *)ptr));
printf("USING sizeof() OPERATOR: %d \n", sizeof(abc));
getch();
}

Is This Answer Correct ?    13 Yes 0 No

struct ptr { int a; char b; int *p; }abc; what is d sizeof structure without using "sizeo..

Answer / avinash dubey

the correct answer in gcc compiler is 12..
concept of structure padding is involved here..

Is This Answer Correct ?    11 Yes 2 No

struct ptr { int a; char b; int *p; }abc; what is d sizeof structure without using "sizeo..

Answer / vishnu948923

void main()
{
int x,y;
y=&abc.a;
x=(&abc->p+1);
printf("%d",x-y);
}

Is This Answer Correct ?    8 Yes 5 No

struct ptr { int a; char b; int *p; }abc; what is d sizeof structure without using "sizeo..

Answer / kk

12 is answer
int a takes 4 bytes
char b takes 4 bytes due to data padding
int *p any pointer takes 4 bytes
so totally 12 bytes........

Is This Answer Correct ?    4 Yes 1 No

struct ptr { int a; char b; int *p; }abc; what is d sizeof structure without using "sizeo..

Answer / gagandeep

The total size would be 5.
Since 'int a' takes 2;
'char b' takes 1; and
'int *p' takes 2 (Note: Any pointer variable would take 2 bytes)

There is no additional size of 'struct' data structure.
Hence 5 is the value.

Is This Answer Correct ?    12 Yes 10 No

struct ptr { int a; char b; int *p; }abc; what is d sizeof structure without using "sizeo..

Answer / ryan

The size of the structure is 5 bytes

Is This Answer Correct ?    5 Yes 4 No

struct ptr { int a; char b; int *p; }abc; what is d sizeof structure without using "sizeo..

Answer / pradeep

In Linux, its 12 bytes.
int a ------- 4 bytes
char b ------- 1 byte.
but as the next element is integer, it wont fit in the
remaining 3 bytes left after the "char b" occupies the first byte of the 4 byte chunk. so these 3 bytes wont be used for storing "int *p". these will be padded. next 4 bytes will be used for storing *p.

to prove it.

int size;
size = (&(abc.p) + sizeof(abc.p)) - &abc.a ;
printf("size = %d",size);

Is This Answer Correct ?    1 Yes 0 No

struct ptr { int a; char b; int *p; }abc; what is d sizeof structure without using "sizeo..

Answer / vignesh1988i

THE SIZE OF THE STRUCTURE WILL BE '5'

Is This Answer Correct ?    4 Yes 7 No

struct ptr { int a; char b; int *p; }abc; what is d sizeof structure without using "sizeo..

Answer / manoj mishra

11

Is This Answer Correct ?    5 Yes 20 No

Post New Answer

More C Interview Questions

how much salary u want ? why u join in our company? your domain is core sector why u prefer software ?

0 Answers   Gopaljee, TCS,


why arithmetic operation can’t be performed on a void pointer?

1 Answers  


what will be the output of this program? void main() { int a[]={5,10,15}; int i=0,num; num=a[++i] + ++i +(++i); printf("%d",num); }

6 Answers   Microsoft,


1 What is a Data Structure?

1 Answers  


char *p="name"; printf(p);

1 Answers  


What does struct node * mean?

0 Answers  


What is the advantage of an array over individual variables?

0 Answers  


What does the function toupper() do?

0 Answers  


what is the output of the program and explain why?? #include<stdio.h> void main ( ) { int k=4,j=0: switch (k) { case 3; j=300; case 4: j=400: case 5: j=500; } printf (“%d\n”,j); }

14 Answers   Oracle,


without using control structures and control structures find the max and min of given 2 nos

1 Answers   HCL,


to find the closest pair

0 Answers   Infosys,


In which layer of the network datastructure format change is done

0 Answers   Honeywell,


Categories