write a program for size of a data type without using
sizeof() operator?

Answers were Sorted based on User's Feedback



write a program for size of a data type without using sizeof() operator?..

Answer / govind279

#include<stdio.h>
int main()
{
int n;
int x,*p,*p1;/* here u can change the type */
p=&x;
p1=(p+1);

printf("size of x is : %d\n",n=(char *)(p1)-(char *)p);
}
Note:without type cast, it always gives 1.
i.e 1 int(4 chars), 1 float(4 chars),1 double(8
chars)etc...coz p+1 points to the next new location of same
type.

Is This Answer Correct ?    40 Yes 9 No

write a program for size of a data type without using sizeof() operator?..

Answer / g

This code will work in TC with 2 warnings but can get result

void main()
{
char *Ptr1,*Ptr2;
float fl;
ptr1 = &fl;
ptr2 = (&fl+1);

printf("%u",ptr2-ptr1);
}

This is a way to get the size of data type...waiting for
any other way...

Is This Answer Correct ?    44 Yes 26 No

write a program for size of a data type without using sizeof() operator?..

Answer / sunil

This has been solved in parts. I am not sure if there are
any better method merging it.

case 1. User passes a variable as the parameter.
eg: int n;
sizeof(n);

case 2. User passes a data type as the parameter.
eg: sizeof(int)


Solution
case 1: #define GetSize(x) (char*)(&x + 1) - (char*)&x

case 2:#define GetMySize(x) (char*)((x*)10 + 1) - (char*)10

Is This Answer Correct ?    15 Yes 6 No

write a program for size of a data type without using sizeof() operator?..

Answer / abdur rab

#include <stdio.h>

struct node {
int x;
int y;
};

unsigned int find_size ( void* p1, void* p2 )
{
return ( p2 - p1 );
}

int main ( int argc, char* argv [] )
{
struct node data_node;
int x = 0;

printf ( "\n The size :%d",
find_size ( (void*) &data_node,
(void*) ( &data_node +
1 ) ) );
printf ( "\n The size :%d", find_size ( (void*) &x,
(void*) ( &x + 1 ) ) );
}

this will work for any data type

Is This Answer Correct ?    15 Yes 8 No

write a program for size of a data type without using sizeof() operator?..

Answer / arun kumar mishra kiit univers

#include<stdio.h>
#include<conio.h>
void main()
{
float f1,*Ptr1,*Ptr2;
ptr1 = &fl;
ptr2 = (&fl+1);
printf("%u",(char *)ptr2-(char *)ptr1);
getch();
}

Is This Answer Correct ?    11 Yes 4 No

write a program for size of a data type without using sizeof() operator?..

Answer / puneet

PLS TYPE ANSWER

Is This Answer Correct ?    16 Yes 12 No

write a program for size of a data type without using sizeof() operator?..

Answer / mukesh kumar singh

#include <iostream>
using namespace std;
struct node {
int x;
int y;
char *s;
};
int main()
{


node x,*p;/* here u can change the type */

p=&x;
cout<<"\n\nSize of node Is : "<<(char*)(p+1)-(char*)p;
return 0;
}

Is This Answer Correct ?    5 Yes 2 No

write a program for size of a data type without using sizeof() operator?..

Answer / lalit kumar

#include "stdafx.h"
#include "stdio.h"
#include "conio.h"
int main()
{
char *a,*s, v='m';
a=&v;
s=a;
a++;
int intsize=(int)a-(int)s;
printf("%d",intsize);
getch();
return 0;
}

Is This Answer Correct ?    3 Yes 0 No

write a program for size of a data type without using sizeof() operator?..

Answer / subham singh

1 #include<iostream>
2 using namespace std;
3 main()
4 {
5 int i;
6 int* p = &i;
7 int* q= p;
8 p++;
9 cout<<(char*)p-(char*)q<<endl;
10 }
11
~

Is This Answer Correct ?    2 Yes 0 No

write a program for size of a data type without using sizeof() operator?..

Answer / anil arya

http://stackoverflow.com/questions/1219199/size-of-a-datatype-without-using-sizeof

Is This Answer Correct ?    2 Yes 1 No

Post New Answer

More C Interview Questions

A array contains dissimilar element how can we count, and A array contains dissimilar element how can we store in another array with out repetition.

4 Answers  


What is this pointer in c plus plus?

0 Answers  


what is dangling pointer?

1 Answers   LG Soft,


write a program to display all prime numbers

0 Answers  


What's the difference between DELETE TABLE and TRUNCATE TABLE commands?

2 Answers   CTC,






Explain how can I remove the trailing spaces from a string?

0 Answers  


we need to calculating INCOME TAX for the person. The INCOME TAX is as follows:- First $10000/- of income : 4% tax Next $10000/- of income : 8% tax Next $10000/- of income : 11.5% tax above $10, 00,00/- : 15% tax What is the Solution of this Question ?

0 Answers  


Is linux written in c?

0 Answers  


write a programme that inputs a number by user and gives its multiplication table.

2 Answers  


can we access one file to one directory?

1 Answers  


who is the editor of 'pokemon'?

1 Answers  


What is d scanf?

0 Answers  


Categories