Thread: sizeof on struct element

  1. #1
    Banned
    Join Date
    Nov 2007
    Posts
    678

    sizeof on struct element

    Code:
    struct data {
        char text[16];
        struct data *next;
    };
    How do I find the sizeof text char array?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Do a sizeof(x.text), where x is a struct data.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need to have an instance of the struct data, e.g.
    Code:
    struct data d;
    printf("sizeof text is %d\n", sizeof(d.text));
    Of course, another option is to use a constant, e.g.
    Code:
    #define TEXTLEN 16
    struct data 
    {
       char text[TEXTLEN];
    ...
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    C99:
    Code:
    #include <stdio.h>
    struct st { char text[30]; };
    int main()
    {
            printf ("sizeof x: %zu\n", sizeof((struct st){0}).text);
    
            return 0;
    }

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    C90?
    Code:
    #include <stdio.h>
    
    struct data {
        char text[16];
        struct data *next;
    };
    
    int main()
    {
       printf ( "size = &#37;lu\n", (long unsigned)sizeof ((struct data*)0)->text );
       return 0;
    }
    
    /* my output
    size = 16
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Nice one Dave.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Not mine -- just a memory. Took a while to jog it, though:
    http://groups.google.com/group/comp....c6ee2af1cce57b
    http://groups.google.com/group/comp....7503f767b1795b

    [edit]There is some deal with offsetof that I think I'm confusing it with though.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Boy!
    So many cryptic ways... thanks to all!

    I find this one easier for me:
    sizeof (<struct instance>.<element name>);

  9. #9
    Banned
    Join Date
    Nov 2007
    Posts
    678
    This is what I was trying to do, reading an unlimited length char array from an input stream:
    Code:
    #ifdef C
    #include <stdio.h>
    
    struct data {
        char text[1024];
        data *next;
    };
    
    data *readData(FILE *in)
    {
        data *d, *tmp;
        d = tmp = new data;
        tmp->next = NULL;
        while (true)
        {
            int n = fread(tmp->text, 1, sizeof(tmp->text)-1, in);
            tmp->text[n] = '\0';
            if (n < (sizeof(tmp->text)-1)) break;    
            tmp->next = new data;
            tmp = tmp->next;
            tmp->next = NULL;
        }
        return d;
    }
    
    void writeData(data *d, FILE *out)
    {
        while (d)
        {
            fputs(d->text, out);
            d = d->next;
        }
    }
    
    int main()
    {
        printf("Enter a really long story please, press Ctrl-Z to end!\n");
        data *d = readData(stdin);
        printf("Your text:\n");
        writeData(d, stdout);
        return 0;
    }
    #else
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        cout << "Enter a really long story please, press Ctrl-Z to end!" << endl;
        string text;
        string line;
        while (getline(cin, line)) text += line + '\n';
        cout << "Your text:" << endl << text;
        return 0;
    }
    #endif
    See how simple and small C++ version is!

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by manav View Post
    Code:
    #ifdef C
    What is that?

  11. #11
    Banned
    Join Date
    Nov 2007
    Posts
    678
    You see, two versions, two main()s of my program are in the same file, so to toggle the compile, I used this directive!

    Side note: I know, my C version will not compile as C file, but it's almost identical to what I would do in C.

  12. #12
    Day Dreamer
    Join Date
    Apr 2007
    Posts
    45
    Code:
    #ifdef C
    #include <stdio.h>
    
    struct data {
        char text[1024];
        data *next;
    };
    
    data *readData(FILE *in)
    {
        data *d, *tmp;
        d = tmp = new data;
        tmp->next = NULL;
        while (true)
        {
            int n = fread(tmp->text, 1, sizeof(tmp->text)-1, in);
            tmp->text[n] = '\0';
            if (n < (sizeof(tmp->text)-1)) break;    
            tmp->next = new data;
            tmp = tmp->next;
            tmp->next = NULL;
        }
        return d;    // That is called SUICIDE!
    }
    You should never return a local pointer! You can never be sure of the consequences.
    I would love to change the world but they dont give me the source code!

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by zombiezparadize View Post
    You should never return a local pointer! You can never be sure of the consequences.
    Rubbish! The only consequence of that example is that one could forget do deallocate the memory returned and cause a memory leak.
    You don't know C++ anywhere near as well as you think you do.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  14. #14
    Banned
    Join Date
    Nov 2007
    Posts
    678
    zombiezparadize! wow! LOL!!! Ha Ha Ha!

    Edit: Was that a little over the top? Sorry. But thanks for a really good laugh
    Last edited by manav; 05-07-2008 at 01:16 AM.

  15. #15
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Edit2: Actually, I realize that zombie is somewhat right, i.e., never return a local pointer, which, contains a local address!
    zombie, the point is do not return the address of local objects, pointer being local is ok, and also i am returning the address obtained from new, which will be available, even after, the function returns.

    [I made this separate post, just to update properly and also my post was last, still, iMalc was listed as last post.]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM
  5. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM