sizeof implementation

Discussion in 'C Programming' started by Debajyoti Sarma, Jul 11, 2010.

  1. 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.
     
    Debajyoti Sarma, Jul 11, 2010
    #1
    1. Advertising

  2. Debajyoti Sarma

    Eric Sosman Guest

    On 7/11/2010 10:59 AM, Debajyoti Sarma wrote:
    > How to implement sizeof operator in C ?


    The same way you implement the `+' operator: It's the
    compiler's business.

    >[...]
    > Function is more preferable than macro.


    Nonsense.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Jul 11, 2010
    #2
    1. Advertising

  3. On 11 July, 15:59, Debajyoti Sarma <> wrote:
    > 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);
     
    Nick Keighley, Jul 14, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Derek
    Replies:
    7
    Views:
    24,368
    Ron Natalie
    Oct 14, 2004
  2. Trevor

    sizeof(str) or sizeof(str) - 1 ?

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    650
    CBFalconer
    Apr 10, 2004
  3. Vinu
    Replies:
    13
    Views:
    1,458
    Lawrence Kirby
    May 12, 2005
  4. blufox

    sizeof( int ) != sizeof( void * )

    blufox, May 22, 2006, in forum: C Programming
    Replies:
    2
    Views:
    567
    Joe Smith
    May 22, 2006
  5. Spiros Bousbouras
    Replies:
    40
    Views:
    1,500
    Keith Thompson
    Jan 20, 2007
Loading...

Share This Page