Thread: Array initializing to a function parameter

  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 04:28 AM.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    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:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

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, 10: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