Fast Array Init help.

This is a discussion on Fast Array Init help. within the C Programming forums, part of the General Programming Boards category; So lets say I have a function which accepts an array: Code: void Foo(int* data); I can pass data to ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    2

    Fast Array Init help.

    So lets say I have a function which accepts an array:

    Code:
    void Foo(int* data);
    I can pass data to it like this:

    Code:
    int[] data = {1,2,3};
    Foo(data);
    But I don't like the way I've got to initialize a variable when I just want to send some static data. What I want to do is call the function like this:
    Code:
    Foo((int[]){1,2,3});
    Except that doesn't work. How can I pass an array to a function without setting it up in a variable beforehand?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why on earth would you want to do that? And how do you expect to know how big your array is once it gets there? I suppose you could use the va_* macros. But that seems like kind of a waste.

    Anyway, the reason that doesn't work is because when arrays get passed to functions, it doesn't actually pass the array. It passes the address of the first element via pointer.


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

  3. #3
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    983
    This is possible in C99, using precisely the syntax you use as an example. They're called compound literals.

    C99, if you're not aware, is the 1999 revision of the previous (1989/1990) C standard. It's not as widely implemented as C89/C90, so if you're looking for maximum portability, you're out of luck. But if you are willing to require a C99-ish compiler (recent versions of gcc, Intel's C compiler, clang, Open64, and Sun Studio, at least, support compound literals), go for it.

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    2
    When dealing with functions that accept arrays of chars I can do this:

    Code:
    Foo("Hello World");
    Which genarates an array of characters and passes its reference to the function all in one line of code. Why can't I do something simmilar with an array of integers?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What you're trying to do isn't the same thing. Arrays of character aren't the same as arrays of integers. String literals make a pointer to a hard coded string, which you can't modify. You don't pass an actual array, you pass a pointer to it.


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

  6. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by quzah View Post
    Arrays of character aren't the same as arrays of integers.
    Since when? Pointer is pointer. OK, the type may vary but apart from that, they behave exactly the same. And whether my function takes a
    Code:
    const char* const
    or a
    Code:
    const int * const
    doesn't make any difference.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    What advantage will you get by being able to do so?

    Code:
    int[] data = {1,2,3};   // java ?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by QuantumPete View Post
    Since when? Pointer is pointer. OK, the type may vary but apart from that, they behave exactly the same.
    I meant you can't do:
    Code:
    foo( "intoneinttwointthreeintfourintfive" );
    They don't behave the same as a string. That's why I said twice that you can't do that in the way he wants. You can't make a quote-contained array of numbers. How would you know what this meant?

    foo( "123343242342342" );

    How many numbers is that? That's why I said you can't do what you want the way you would with a string of characters. See, with a string, each byte is its own number. Not so with multi-byte values.


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

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Quote Originally Posted by kramerr View Post
    But I don't like the way I've got to initialize a variable when I just want to send some static data. What I want to do is call the function like this:
    Code:
    Foo((int[]){1,2,3});
    Except that doesn't work. How can I pass an array to a function without setting it up in a variable beforehand?
    Like it or not, you simply can't do that with ints in C.
    C++ on the other hand, is a different story.
    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"

  10. #10
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Quote Originally Posted by iMalc View Post
    Like it or not, you simply can't do that with ints in C.
    C++ on the other hand, is a different story.
    You can if you have C99 compiler. cas has already pointed out.

  11. #11
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Could you be more specific what "doesn't work"?

    An error message or a description of the perceived/expected/experienced behavior would be a good start.

    (I didn't have to force C99 mode on GCC 4.4.0 to pass compound literals as function arguments.)

  12. #12
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    983
    (I didn't have to force C99 mode on GCC 4.4.0 to pass compound literals as function arguments.)
    That would be because gcc, by default, is neither a C90 nor a C99 compiler; rather, it compiles a language known as GNU C, which is a mix of C90, C99, and extensions. So some C99 features work by default in gcc, but others don't.

  13. #13
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by cas View Post
    That would be because gcc, by default, is neither a C90 nor a C99 compiler; rather, it compiles a language known as GNU C, which is a mix of C90, C99, and extensions. So some C99 features work by default in gcc, but others don't.
    I wasn't aware of that tidbit. Cheers!

  14. #14
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    I don't like the way I've got to initialize a variable when I just want to send some static data.
    You just need to be able to create unnamed arrays. It needs compiler support. But it doesn't give you any speed or code size benefit.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  15. #15
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Quote Originally Posted by Bayint Naung View Post
    You can if you have C99 compiler. cas has already pointed out.
    OH, I didn't see that post sorry. Glad to stand corrected.

    HOLY CRAP!
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. FAST method to copy char array?
    By MitchellH in forum C++ Programming
    Replies: 4
    Last Post: 10-09-2005, 08:19 AM
  2. Array of structures in a class, init
    By seizmic in forum C++ Programming
    Replies: 13
    Last Post: 09-17-2005, 01:12 PM
  3. how to init 2d array ?
    By black in forum C# Programming
    Replies: 16
    Last Post: 04-25-2004, 04:33 PM
  4. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  5. two dimensional dynamic array?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 04:27 PM

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