Operating System - OpenVMS
1822895 Members
4124 Online
109645 Solutions
New Discussion юеВ

Alternative for sizeof operator

 
SOLVED
Go to solution
WSSPL
Frequent Advisor

Alternative for sizeof operator

Hi All,

Is there any alternative to find the size of
data type like int, char apart from sizeof() operator.

Thanks,
Poornima.



IT IS THE START THAT STOPS MOST PEOPLE
5 REPLIES 5
Mike Naime
Honored Contributor

Re: Alternative for sizeof operator

How about a little more info here?

What (programing) language are you talking about?
VMS SAN mechanic
WSSPL
Frequent Advisor

Re: Alternative for sizeof operator

Hi,

I am talking about C programming language.

Thanks,
Poornima
IT IS THE START THAT STOPS MOST PEOPLE
Antoniov.
Honored Contributor

Re: Alternative for sizeof operator

Hi Poornima,
No there is not alternative to sizeof() operator in standard ANSI C.
Sizeof() is a compiletime operator can be applied to type too ,i.e. sizeof(int); runtime not call any function so sizeof() is very quick.
Why do you will not use sizeof() operator?

Antoniov
Antonio Maria Vigliotti
WSSPL
Frequent Advisor

Re: Alternative for sizeof operator

Hi,

Actually, this is a tricky question asked
by one of my friends who is proficient is C language.

Thanks,
Poornima.
IT IS THE START THAT STOPS MOST PEOPLE
Antoniov.
Honored Contributor
Solution

Re: Alternative for sizeof operator

Hi Poormina,
if it's only a tricly question, you can use other then sizeof as follow:

#include
#define mysize sizeof
main ()
{
unsigned short size_of;
long iMyVar, *piMyVar;

piMyVar = iMyVar;
size_of = ((unsigned long) (piMyVar + 1))
- ((unsigned long) (piMyVar));
printf("%d %d %d",mysize(iMyVar), size_of, sizeof(iMyVar));
}

Above program run correctly (you can try).
1.st is a macro that redefines a sizeof operator.
In second statetemnt (size_of = ...), program evaluate address of a variable and next location; every address is casted into long and different is located in size_of variable.
Look that without unsigned long casting, different it equal to 1!!



H.T.H.
Antoniov





Antonio Maria Vigliotti