sizeof implementation

D

Debajyoti Sarma

How to implement sizeof operator in C ?
cases
1. int i=0; sizeof(i)=4 ...i.e size of variables
2. sizeof(int)=4 ...i.e size of datatypes
3. struct t
{
int a;
char c;
}p;
sizeof(p)=5 ....i.e it should work for user
define data types
sizeof(t)=5

Function is more preferable than macro.
 
N

Nick Keighley

How to implement sizeof operator in C ?
cases
1. int i=0; sizeof(i)=4         ...i.e size of variables
2. sizeof(int)=4                 ...i.e size of datatypes
3. struct t
   {
   int a;
   char c;
   }p;
    sizeof(p)=5                  ....i.e it should work for user
define data types
    sizeof(t)=5

Function is more preferable than macro.

the correct answer is "sizeof is a built in operator of a C
implementaion. In general it cannot be implemented in standard C
code". That's *why* they built it in!

But to do your homework consider the result of running a program
containign code like this

int i,
int *pi = &i;
printf ("%p %p\n", pi, pi + 1);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top