Array initializing to a function parameter

This is a discussion on Array initializing to a function parameter within the C++ Programming forums, part of the General Programming Boards category; Hi all..I have a little situation that I'd really like to know. I could have for example struct Something { ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    24

    Array initializing to a function parameter

    Hi all..I have a little situation that I'd really like to know.
    I could have for example

    struct Something {
    int arr[7];

    Something(int _arr[]) {...}
    ~Something() {}
    };

    or

    int arr[5] and f(int _arr[]).

    So my question goes, how would u initialize arr[] during the call of f() so that f() gets the initialized values?

    I've tried normal initializing with {}:

    Something strange( Something({0, 3, 6, 7, 2}) );

    Compiling this causes an error. That's all i know for now?
    U'd be a big help with my project, thanks
    Last edited by JulleH; 01-17-2005 at 03:28 AM.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    you need to provide the name of the struct/class and remember to use a second set of brackets for arrays in the structure:

    Code:
    struct foo {
     int i;
     float j[3];
     };
    
    void bar(foo f) {
    
     } 
     
    int main() {
     bar((struct foo){1, { 2.0, 3.0, 4.0 } });
     }
    for plain arrays you should be able to do:

    Code:
    void bar(int array[]) {
    
     } 
     
    int main() {
     bar((int[]){10, 12, 3});
     }
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    24
    hmm I copied your bar code and compiled it and I got an expression syntax error...and i'm wondering if that should work with all compilers, even with my brcc32.exe (i didn't find the version number)?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    Compound literals are a new feature of C99, and there are only a couple of compilers which support it.

    C++ does not have this feature
    http://david.tribble.com/text/cdiffs...mpound-literal

    So the safe thing is to initialise a variable with the data you want, then pass that variable to the function in question.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  4. pointer to array as parameter to function
    By Jurgen in forum C++ Programming
    Replies: 1
    Last Post: 05-20-2004, 02:51 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

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