Thread: sizeof

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


    This is probably a religious question, but here goes:

    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.


  2. #2
    Registered User
    Join Date
    Sep 2004
    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
    Toronto, Canada
    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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Portland, OR
    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:

    int n = sizeof int;
    Yet another reason to prefer the parentheses, because it works for both types and expressions.
    	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
    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.

    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:

    My website:
    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, 01:09 AM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. finding size of empty char array
    By darsunt in forum C Programming
    Replies: 12
    Last Post: 05-30-2006, 07:23 PM
  4. Cutting up an std::string
    By Shamino in forum C++ Programming
    Replies: 26
    Last Post: 04-04-2006, 11:02 AM
  5. Model not showing up on screen...
    By Shamino in forum Game Programming
    Replies: 14
    Last Post: 03-09-2006, 08:00 PM