Write a c program to find the size of structure without using sizeof operator






How to find size of structure data type without using sizeof operator in c programming language  


#include<stdio.h>

struct student{
    int roll;
    char name[100];
    float marks;
};

int main(){

  struct student *ptr = 0;

  ptr++;
  printf("Size of the structure student:  %d",ptr);

  return 0;
}





1. Write a c program to convert decimal number to hexadecimal number.
3. Write a c program to convert octal number to decimal number.
4. Write a c program to convert octal number to hexadecimal number.
5. Write a c program to convert hexadecimal number to decimal number.
6. Write a c program to convert hexadecimal number to octal number.
8. Write a c program to convert binary number to hexadecimal number.
9. Write a c program to convert binary number to octal number.
11. Write a c program to convert hexadecimal number to binary number.
12. Write a c program to convert octal number to binary number.
14. Write a c program to convert centigrade to fahrenheit.

6 comments:

Rajiv kashyap said...

Can any1 please explain above program????

Tim said...

when you create pointer to structure lets say

struct student *ptr;

it will assign some memory address to its members such that

address of-

&ptr->roll 36857 the starting address
&ptr->name 36859 ( 36857 the starting address of roll + 2 bytes = 36859 the starting address of char name[100];)
&ptr->marks 36959 ( 36859 the starting address of name + 100 bytes = 36959 the starting address of float marks;)

so the address range for all members are 36857-36962 (coz the address range of marks is 36959 -36962 which is of 4 bytes).

ok now if you use statement as following like in above example

struct student *ptr=0;


it will assign starting address to 0 (zero) instead of 36857 such that

address of-

&ptr->roll 0
&ptr->name 2
&ptr->marks 102

so the address range of all members of structure will be now from 0 to 105 ( because marks will be stored at address 102-105 which is 4 bytes)

so ptr++ will increment to the starting address 106 of next structure variable which is equal the total size of structure.

hope this helps ...

Unknown said...

Thanks Tim

jay said...

the address range for char 102 to 105. how 105 is coming (adding 3 bytes more). Can anyone please explain??

Darshi said...

What will be the output of this program?plz tell any1

Unknown said...

When we increment the pointer then pointer increases a block of memory (block of memory depends on pointer data type). So here we will use this technique to calculate the size of a structure.