sizeof

This is a discussion on sizeof within the C Programming forums, part of the General Programming Boards category; This is probably a religious question, but here goes: Code: struct something { int x, y, z; } struct something ...

  1. #1
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065

    sizeof

    This is probably a religious question, but here goes:

    Code:
    struct something {
            int x, y, z;
    }
    
    struct something *buf;
    ...
    A:printf("something about size of *buf %i.\n", sizeof *buf);
    B:printf("something about size of *buf %i.\n", sizeof(*buf));
    ...
    SO, given the above, what is the general concensus about A vs B? Which way is the "right" way? Again, this is probably a religious discussion.

    Andy

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    This depends on if you look at sizeof as more of an operator, or a function. I always preferred the latter. At any rate, I don't think there is a "right" way.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Personal preference I guess. There may be precedence rules that would necessitate the use of parenthesis in some cases. I always use them as if sizeof was a function. I know it's rather more of a compiler pre-processor thing and can be resolved early. But then why are parenthesis necessary for SWITCH statements, or IF statements if the statement is simple and unambiguous enough they shouldn't be necessary.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    To muddle the issue further, you NEED the parentheses if the argument to sizeof is a type (as opposed to an expression). This is not valid C:

    Code:
    int n = sizeof int;
    Yet another reason to prefer the parentheses, because it works for both types and expressions.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    I used to use parentheses all of the time, but there's a certain quality of laziness in being able to leave them out. Sometimes it can also make your code more readable if you already have a few levels of parentheses.

    You just have to keep in mind that parentheses are required for types, as mentioned. But usually I think that if you're taking the size of a type you're not doing something correctly. Either take the size of a variable or use limits.h.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Malloc,calloc..Sscanf.
    By ozumsafa in forum C Programming
    Replies: 22
    Last Post: 07-26-2007, 02:09 AM
  2. Replies: 14
    Last Post: 06-28-2006, 02:58 AM
  3. finding size of empty char array
    By darsunt in forum C Programming
    Replies: 12
    Last Post: 05-30-2006, 08:23 PM
  4. Cutting up an std::string
    By Shamino in forum C++ Programming
    Replies: 26
    Last Post: 04-04-2006, 12:02 PM
  5. Model not showing up on screen...
    By Shamino in forum Game Programming
    Replies: 14
    Last Post: 03-09-2006, 08:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21