sizeof with structures

This is a discussion on sizeof with structures within the C Programming forums, part of the General Programming Boards category; lets say i have this structure Code: struct example{int x, float y[2]}z[12],j[5]; if i say Code: sizeof(example) ; which size ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    65

    sizeof with structures

    lets say i have this structure

    Code:
    struct example{int x, float y[2]}z[12],j[5];
    if i say
    Code:
    sizeof(example) ;
    which size from all the above will return

    i have one last query if i send to one function this:
    Code:
    function_example( (void*)z);
    what actually i send here?
    thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    sizeof(example), funnily enough, will give you the size of (one) example (object).

    In the second case, you are sending z (the array of examples, as a pointer to its first element) to your function.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    65
    thanks dude
    have a nice day

  4. #4
    1337
    Join Date
    Jul 2008
    Posts
    135
    Is there any way to know the real size of a struct?

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    sizeof (structname);

    Really... you can easily look these things up in your C library documentation...

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by CommonTater View Post
    sizeof (structname);

    Really... you can easily look these things up in your C library documentation...
    There you go again telling people to look stuff up. Honestly, I don't know how you live with yourself.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    1337
    Join Date
    Jul 2008
    Posts
    135
    Quote Originally Posted by CommonTater View Post
    sizeof (structname);

    Really... you can easily look these things up in your C library documentation...
    From what i understood from the above post, this will return the size of one of the element in the struct, which is not the whole struct.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    How about you read like Tater suggested, and try fiddling around with a sample program. Given the code from your first post, some interesting things to print out might be:
    • sizeof(struct example)
    • sizeof(z)
    • sizeof(z[0])
    • sizeof(z[0].x)
    • sizeof(z[0].y)
    • sizeof(z[0].y[0])
    • sizeof(j)


    EDIT:
    > From what i understood from the above post, this will return the size of one of the element in the struct, which is not the whole struct.
    And how did you come to this conclusion? You asked a question and Tater gave you an answer, and a correct one at that. How did you infer you're only getting one element of the struct?
    Last edited by anduril462; 08-23-2011 at 01:42 PM.

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by tabstop View Post
    sizeof(example), funnily enough, will give you the size of (one) example (object).
    Quote Originally Posted by valthyx View Post
    From what i understood from the above post, this will return the size of one of the element in the struct, which is not the whole struct.
    Oh, my bad, I didn't even see tabstop's post. tabstop mispoke, the sizeof operator will always return the number of bytes for any operand. This is true for structures, arrays, native types ect.

    So for sizeof(example) and sizeof(structname) will both return the same number; the size of the type in bytes.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    sizeof(struct example) will give you the size of one struct example object, since that's what you asked for, the size of an example.

    If you want (say) the size of the array z (in bytes, not in objects), then you can do sizeof(z).

  11. #11
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by tabstop View Post
    sizeof(struct example) will give you the size of one struct example object, since that's what you asked for, the size of an example.
    ...in bytes.

    EDIT: Oops...I guess I was the one confused about what the OP was asking.
    Last edited by AndrewHunter; 08-23-2011 at 03:03 PM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by valthyx View Post
    From what i understood from the above post, this will return the size of one of the element in the struct, which is not the whole struct.
    It returns the entire size of one struct or union ...

    If you have an array of structs... just multiply by the number of elements in the array.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    ...in bytes.

    EDIT: Oops...I guess I was the one confused about what the OP was asking.
    I don't think so... I'm thinking our OP friend has confused "struct" with "array"... in particular by calling an array of structs a struct.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structures, fread(), sizeof()
    By RyanLeonard in forum C++ Programming
    Replies: 4
    Last Post: 05-19-2011, 03:36 AM
  2. Replies: 5
    Last Post: 12-09-2010, 02:33 PM
  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. Replies: 5
    Last Post: 04-11-2002, 12:29 PM

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