top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to find size of structure without using sizeof operator in C?

+2 votes
878 views
How to find size of structure without using sizeof operator in C?
posted Jun 2, 2015 by Mohammed Hussain

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+2 votes
 
Best answer

Evaluate Size of Structure Without using Sizeof Operator.

Sizeof operator is used to evaluate the size of any data type or any variable in c programming. Using sizeof operator is straight forward way of calculating size but following program will explain you how to calculate size of structure without using sizeof operator.

#include<stdio.h>

struct
  {
  int num1,num2;
  char s1;
  int *ptr;
  int abc[5];
}a[2];

void main()
  {
  int start,last;
  start = &a[1].num1;
  last = &a[0].num1;
  printf("\nSize of Structure : %d Bytes",start-last);
}

Output :

Size of Structure : 17 Bytes
answer Jun 2, 2015 by Shivaranjini
+1 vote
#include <stdio.h>
typedef  struct
{
   int a;
   int b;
}str;


int main()
{
   str x;
   str *ptr  = &x;

   printf("%u", (char*)(ptr+1) - (char*)ptr);

   return 0;
}

Output = 8

answer Jun 7, 2015 by Vikram Singh
Similar Questions
+2 votes

How can I wwap value two variables without using third variable or +/- operator?

+4 votes
printf ("%s %d %f\n", (*ptr+i).a, (*ptr+i).b, (*ptr+i).c);
[Error] invalid operands to binary + (have 'struct name' and 'int')

where ptr is a pointer to the structure and allocated dynamic memory using malloc. Any suggestion would be helpful.

Thanks in advance.

...