sizeof operator question

Discussion in 'C Programming' started by Daniel Rudy, Jan 26, 2007.

  1. Daniel Rudy

    Daniel Rudy Guest

    Hello Everyone,

    I've ran into a little snag with the sizeof operator. Can you get the
    size of a member inside a struct using sizeof? I looked through the FAQ
    and I couldn't find the answer to this one.

    Below is the code fragment. Granted it's pretty platform specific.


    typedef struct hwata_info_tag
    {
    char device[20];
    char model[40];
    char fwrev[8];
    char serial[20];
    } hwata_info_t;


    /* opens the first ata harddisk on the system
    and returns information about it. */
    int hwata_get_info(hwata_info_t *atainfo)
    {
    int fd;
    int result;
    struct ata_params params;
    const char device[] = "/dev/ad0";

    fd = open(device, O_RDONLY);
    if (fd < 0)
    {
    ERR("open device failed", errno, 1)
    return(errno);
    }
    result = ioctl(fd, IOCATAGPARM, &params);
    if (result < 0)
    {
    ERR("failed to get device information", errno, 1)
    return(errno);
    }
    close(fd);
    strlcpy(atainfo->device, device, sizeof(atainfo->device));
    strlcpy(atainfo->model, params->model, sizeof(atainfo->model));
    strlcpy(atainfo->serial, params->serial, sizeof(atainfo->serial));
    strlcpy(atainfo->fwrev, params->revision, sizeof(atainfo->fwrev));
    return(0);
    }


    And here is the compiler result:

    strata:/home/dr2867/c/modules 877 $$$ ->./compile hwata hwata.c error.o
    syslog.o param.o

    Output File: hwata

    Input Files:
    hwata.c
    error.o
    syslog.o
    param.o

    gcc -W -Wall -Wshadow -Wpointer-arith -Wcast-align -Wstrict-prototypes
    -Wnested-externs -Wwrite-strings -Wflo
    at-equal -Winline -Wtrigraphs -ansi -std=c89 -pedantic -ggdb3 -o hwata
    hwata.c error.o syslog.o param.o
    hwata.c: In function `hwata_get_info':
    hwata.c:82: error: invalid type argument of `->'
    hwata.c:83: error: invalid type argument of `->'
    hwata.c:84: error: invalid type argument of `->'

    strata:/home/dr2867/c/modules 878 $$$ ->



    So if this doesn't work, is there another way of doing this?


    --
    Daniel Rudy

    Email address has been base64 encoded to reduce spam
    Decode email address using b64decode or uudecode -m

    Why geeks like computers: look chat date touch grep make unzip
    strip view finger mount fcsk more fcsk yes spray umount sleep
     
    Daniel Rudy, Jan 26, 2007
    #1
    1. Advertising

  2. Daniel Rudy wrote:

    >
    > I've ran into a little snag with the sizeof operator. Can you get the
    > size of a member inside a struct using sizeof?


    Yes, sizeof works on just about anything.

    > [...]
    > struct ata_params params;
    > [...]
    > strlcpy(atainfo->model, params->model, sizeof(atainfo->model));
    > strlcpy(atainfo->serial, params->serial, sizeof(atainfo->serial));
    > strlcpy(atainfo->fwrev, params->revision, sizeof(atainfo->fwrev));
    > [...]
    > hwata.c:82: error: invalid type argument of `->'
    > hwata.c:83: error: invalid type argument of `->'
    > hwata.c:84: error: invalid type argument of `->'


    The compiler isn't complaining about the use of sizeof. It's
    complaining about using the "->" operator with the params variable,
    since params is a struct (not a pointer to a struct). Replace
    "params->" with "params." and the compiler should be happy.
     
    Steve Kirkendall, Jan 26, 2007
    #2
    1. Advertising

  3. Daniel Rudy

    santosh Guest

    Daniel Rudy wrote:
    > Hello Everyone,
    >
    > I've ran into a little snag with the sizeof operator. Can you get the
    > size of a member inside a struct using sizeof? I looked through the FAQ
    > and I couldn't find the answer to this one.


    Yes, mostly you can.

    > Below is the code fragment. Granted it's pretty platform specific.

    <snip code>

    > And here is the compiler result:

    <snip>
    > gcc -W -Wall -Wshadow -Wpointer-arith -Wcast-align -Wstrict-prototypes
    > -Wnested-externs -Wwrite-strings -Wflo
    > at-equal -Winline -Wtrigraphs -ansi -std=c89 -pedantic -ggdb3 -o hwata
    > hwata.c error.o syslog.o param.o
    > hwata.c: In function `hwata_get_info':
    > hwata.c:82: error: invalid type argument of `->'
    > hwata.c:83: error: invalid type argument of `->'
    > hwata.c:84: error: invalid type argument of `->'


    The identifier params seems to be a struct object, not a pointer to
    one, so you should use the dot operator to access the members like:

    x = params.member;

    > So if this doesn't work, is there another way of doing this?


    The sizeof statements should work. The errors are over another mistake,
    as pointed above.
     
    santosh, Jan 26, 2007
    #3
  4. "santosh" <> wrote in message
    news:...
    > Daniel Rudy wrote:
    >> Hello Everyone,
    >>
    >> I've ran into a little snag with the sizeof operator. Can you get the
    >> size of a member inside a struct using sizeof? I looked through the FAQ
    >> and I couldn't find the answer to this one.

    >
    > Yes, mostly you can.


    *mostly*

    when I read the first sentence I thought it was gonna be a question like why
    the following code doesnt work:

    struct X
    {
    char buf[sizeof(struct X)];
    };

    Thats one of the cases where sizeof cant work (of course)
     
    Serve Laurijssen, Jan 26, 2007
    #4
  5. Daniel Rudy

    CBFalconer Guest

    Serve Laurijssen wrote:
    > "santosh" <> wrote in message
    >> Daniel Rudy wrote:
    >>
    >>> I've ran into a little snag with the sizeof operator. Can you
    >>> get the size of a member inside a struct using sizeof? I looked
    >>> through the FAQ and I couldn't find the answer to this one.

    >>
    >> Yes, mostly you can.

    >
    > *mostly*
    >
    > when I read the first sentence I thought it was gonna be a
    > question like why the following code doesnt work:
    >
    > struct X
    > {
    > char buf[sizeof(struct X)];
    > };
    >
    > Thats one of the cases where sizeof cant work (of course)


    sizeof is working fine there. However struct X is undefined.

    --
    <http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>

    "A man who is right every time is not likely to do very much."
    -- Francis Crick, co-discover of DNA
    "There is nothing more amazing than stupidity in action."
    -- Thomas Matthews
     
    CBFalconer, Jan 26, 2007
    #5
  6. Daniel Rudy

    Ben Pfaff Guest

    CBFalconer <> writes:

    > Serve Laurijssen wrote:
    >> struct X
    >> {
    >> char buf[sizeof(struct X)];
    >> };
    >>
    >> Thats one of the cases where sizeof cant work (of course)

    >
    > sizeof is working fine there. However struct X is undefined.


    No, it's not undefined--it's incomplete. See the words of the
    standard (C99 6.7.2.1):

    A structure or union shall not contain a member with
    incomplete or function type (hence, a structure shall not
    contain an instance of itself, but may contain a pointer to
    an instance of itself)...

    ...The [structure] type is incomplete until after the }
    that terminates the [member] list.
    --
    "Some programming practices beg for errors;
    this one is like calling an 800 number
    and having errors delivered to your door."
    --Steve McConnell
     
    Ben Pfaff, Jan 26, 2007
    #6
  7. Daniel Rudy

    Daniel Rudy Guest

    At about the time of 1/26/2007 9:40 AM, Steve Kirkendall stated the
    following:
    > Daniel Rudy wrote:
    >
    >> I've ran into a little snag with the sizeof operator. Can you get the
    >> size of a member inside a struct using sizeof?

    >
    > Yes, sizeof works on just about anything.
    >
    >> [...]
    >> struct ata_params params;
    >> [...]
    >> strlcpy(atainfo->model, params->model, sizeof(atainfo->model));
    >> strlcpy(atainfo->serial, params->serial, sizeof(atainfo->serial));
    >> strlcpy(atainfo->fwrev, params->revision, sizeof(atainfo->fwrev));
    >> [...]
    >> hwata.c:82: error: invalid type argument of `->'
    >> hwata.c:83: error: invalid type argument of `->'
    >> hwata.c:84: error: invalid type argument of `->'

    >
    > The compiler isn't complaining about the use of sizeof. It's
    > complaining about using the "->" operator with the params variable,
    > since params is a struct (not a pointer to a struct). Replace
    > "params->" with "params." and the compiler should be happy.


    That did correct the problem. Got a little too used to using pointers
    to structures there. Thanks for pointing it out.

    --
    Daniel Rudy

    Email address has been base64 encoded to reduce spam
    Decode email address using b64decode or uudecode -m

    Why geeks like computers: look chat date touch grep make unzip
    strip view finger mount fcsk more fcsk yes spray umount sleep
     
    Daniel Rudy, Jan 27, 2007
    #7
    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,339
    Ron Natalie
    Oct 14, 2004
  2. Trevor

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

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    634
    CBFalconer
    Apr 10, 2004
  3. Manish_Ganvir
    Replies:
    13
    Views:
    1,583
    Keith Thompson
    Feb 14, 2005
  4. Vinu
    Replies:
    13
    Views:
    1,428
    Lawrence Kirby
    May 12, 2005
  5. Kislay

    The sizeof operator : sizeof(++i)

    Kislay, Oct 18, 2007, in forum: C Programming
    Replies:
    10
    Views:
    688
    Peter Pichler
    Oct 19, 2007
Loading...

Share This Page