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
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 |
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 |
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 |
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 |
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 |
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 |
logic for x=y^n
Explain goto?
what is the main use of c where it can use the c
Explain what are the standard predefined macros?
When do you not use the keyword 'return' when defining a function a) Always b) Never c) When the function returns void d) dfd
program to find out date after adding 31 days to a date in the month of febraury also consider the leap year
What is the purpose of realloc()?
What are the different categories of functions in c?
What would happen to X in this expression: X += 15; (assuming the value of X is 5)
is forign key will be unique key any table or not?
Define a structure to store the record of library. The record must consist of at least following fields: Title, Author, Edition, Price, Publisher, and Category. -Define functions authorSearch ( ), TitleSearch ( ) and CategorySearch ( ) to search a book with respect to author, title and category. [There can be more than one book, written by one author, in one category]
which of the following is not a character constant a) 'thank you' b) 'enter values of p, n ,r' c) '23.56E-o3' d) all of the above