Thread: Array size referencing struct member

  1. #1
    Registered User Russell's Avatar
    Join Date
    May 2004
    Posts
    71

    Array size referencing struct member

    Is it possible to do the following:
    Code:
    136 struct s
    137 {
    138     int a;
    139     char b[a];
    140 };
    I get the following error:
    Code:
    file.h:138: error: invalid use of non-static data member `s::a'
    file.h:139: error: from this location

  2. #2
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    The bad way to do it would be to make the variable a global. So don't do that. Instead you could use a either number:

    Code:
    struct s 
    {
    	char b[120];
    };
    Or - the way I would do it - use a string.

    Code:
    struct s 
    {
    	string b;
    };
    You could use a vector as well.

    I may be completely wrong i'm just tapping from the top of my head so experiment
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  3. #3
    Registered User Russell's Avatar
    Join Date
    May 2004
    Posts
    71
    The reason I don't want to use a string is because the data is binary and may have null characters.

  4. #4
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Try

    Code:
    struct s
    {
    	vector<char> b;
    };
    May be what you're looking for
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  5. #5
    Registered User Russell's Avatar
    Join Date
    May 2004
    Posts
    71
    It looks like std::string will work. Thanks for your input, ahluka.

  6. #6
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Anytime
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  7. #7
    *this
    Join Date
    Mar 2005
    Posts
    498
    Quote Originally Posted by OP
    Code:
    struct s
    137 {
    138     int a;
    139     char b[a];
    140 };
    The reason why your compiler is complaining is because a is not initialized to anything, so b will become a very large array from a garbage value. Also when you change a, it will change the size of b and you cannot change sizes of indexing arrays.

  8. #8
    *this
    Join Date
    Mar 2005
    Posts
    498
    Code:
    The reason I don't want to use a string is because the data is binary and may have null characters.
    Strings can handle null characters.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Um no it can't. Not more than one it can't. A string by definition ends in a null. You can't use the string class for plain old binary data that may potentially contain more than one null. The null terminates the string. It can't have more than one. Other wise all of the string length functions and such will vomit when they hit the first null, considering the string as ended.

    Someone feel free to show otherwise, but I'm fairly sure the string class considers a null character to be "the" end.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    I don't know exactly what the C++ standard says about the string class. But generally speaking, implementations of the string class do not use a null-terminator to mark the end. For example:

    Code:
    #include <iostream>
    #include <string>
    using std::string; using std::endl; using std::cout;
    
    int main()
    {
    
    	string x("abcdefghijklmnop");
    
    	x[5] = 0;
    
    	cout << x << endl;
    
    }
    This will output
    Code:
    abcde ghijklmnop
    on some terminals. Other terminals might behave differently with the zero character; cygwin Xterm is displaying
    Code:
    abcdeghijklmnop
    If I reroute stdout to a file, then NUL is clearly and obviously written to the file. C++ string classes generally do not use strlen when you call .size().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  2. Trouble with DMA Segmentation Faults
    By firestorm717 in forum C Programming
    Replies: 2
    Last Post: 05-07-2006, 09:20 PM
  3. memory allocation for flexible array member of struct
    By jcarouth in forum C Programming
    Replies: 3
    Last Post: 09-11-2005, 12:19 PM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM