Don't understand this 'sizeof' expression.

This is a discussion on Don't understand this 'sizeof' expression. within the C Programming forums, part of the General Programming Boards category; This is copied from my C book: "Finally, if data is defined as an array of struct dataEntry elements, the ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    40

    Don't understand this 'sizeof' expression.

    This is copied from my C book: "Finally,
    if data is defined as an array of struct dataEntry elements, the expression
    sizeof (data) / sizeof (struct dataEntry)
    gives the number of elements contained in data (data must be a previously defined"

    I don't get how that gives the # of elements contained in data. Isn't that dividing the size of data by the size of data entry structure? If so, how does that give the # of elements in data?

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Probably you should make a small program and print to screen what sizeof(data) gives and what sizeof(struct dataEntry) gives
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    40
    How are data and dataEntry defined?

    struct dataEntry
    .
    .
    .

    struct dataEntry data?

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Run this
    Code:
    #include <stdio.h>
    
    struct foo{
            int data;
    };
    
    int main(void)
    {
            struct foo array[] = {{0}, {1}, {2}, {3}, {4}, {5}};
            printf("%u\n", sizeof(struct foo));
            printf("%u\n", sizeof(array));
            printf("%u\n", sizeof(array)/sizeof(struct foo));
    
            return 0;
    }
    Notice that I am not giving you the output, because we should learn to try things by yourself
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,832
    It's more conveniently (and usefully) written as
    sizeof(array)/sizeof(array[0])
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,220
    I usually use this macro:
    Code:
    #define NELEMS(obj) (sizeof(obj)/sizeof(*obj))
    Then you can just write NELEMS(array)

  7. #7
    Registered User
    Join Date
    Jan 2013
    Posts
    40
    Quote Originally Posted by c99tutorial View Post
    I usually use this macro:
    Code:
    #define NELEMS(obj) (sizeof(obj)/sizeof(*obj))
    Then you can just write NELEMS(array)
    Can you explain what that means? I still don't get it. Doesn't that always give 1.

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    1,220
    Suppose you write this

    Code:
    char buf[1000];
    printf("%d\n", NELEMS(buf));
    sizeof buf is 1000 and sizeof *buf is 1, so the result is 1000 elements. The nice thing about the macro is that it works for any object like an array of structs or doubles, etc.

  9. #9
    Registered User
    Join Date
    Jan 2013
    Posts
    40
    Quote Originally Posted by c99tutorial View Post
    Suppose you write this

    Code:
    char buf[1000];
    printf("%d\n", NELEMS(buf));
    sizeof buf is 1000 and sizeof *buf is 1, so the result is 1000 elements. The nice thing about the macro is that it works for any object like an array of structs or doubles, etc.
    Wiat why is sizeof(*buf)=1? Was that something defined?

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Quote Originally Posted by pyroknife View Post
    Wiat why is sizeof(*buf)=1? Was that something defined?
    sizeof(*buf) is essentially sizeof(char), which is guaranteed to be 1 in all cases.
    My best code is written with the delete key.

  11. #11
    Registered User
    Join Date
    Nov 2012
    Posts
    1,220
    Quote Originally Posted by pyroknife View Post
    Wiat why is sizeof(*buf)=1? Was that something defined?
    In C, aggregates are treated as pointers. So buf is a pointer to char, *buf is a char, and sizeof(char) is 1, of course. If buf is an array of something else, then the size will change accordingly.

    Code:
    double buf[100];
    printf("%d\n", NELEMS(buf));

  12. #12
    Registered User
    Join Date
    Jan 2013
    Posts
    40
    Oh okay, but did you have to define *buf=char?

  13. #13
    Registered User
    Join Date
    Nov 2012
    Posts
    1,220
    You already did.

    Code:
    char buf[100];
    If you reference *buf within this scope, the compiler knows that *buf is a char, and it also knows that sizeof buf is 100.

  14. #14
    Registered User
    Join Date
    Jan 2013
    Posts
    40
    Oh thank you.

    What if we defined:
    int buf[100]

    What would be the size of *buf now?

  15. #15
    Registered User
    Join Date
    Jan 2013
    Posts
    40
    Never mind, I don't think my previous question can be answered since a variable int can have many different sizes.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. initializer expression list treated as compound expression
    By karthikeyanvisu in forum C Programming
    Replies: 7
    Last Post: 02-26-2011, 05:19 PM
  2. Replies: 2
    Last Post: 11-25-2009, 07:38 AM
  3. Replies: 6
    Last Post: 10-15-2007, 09:05 AM
  4. sizeof(cin) and sizeof(cout)
    By noobcpp in forum C++ Programming
    Replies: 11
    Last Post: 06-30-2007, 12:00 PM
  5. sizeof and Expression Evaluation
    By Dave_Sinkula in forum C Programming
    Replies: 2
    Last Post: 08-11-2003, 08:11 PM

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