Thread: Passing arrays explicitly

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    88

    Passing arrays explicitly

    I was wondering if you could somehow pass an array to a function like you can in some other languages (like perl).

    For example, say you had the function below:
    Code:
    void getVector( std::vector<std::string>& array ) { return; }
    Could you call that function in any way remotely similar to this (in concept, not style)?
    Code:
    getVector( { "this is a string", "and another", "and on and on" } );
    I ask because I think I remember seeing something like this before, in c++ code, but from the lack of information on it I'm thinking that this just isn't possible. I'd also like to use this, for readabilities sake.

  2. #2
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Can't be done like you have it, the closest I can think of is like:
    Code:
    #include <string>
    #include <vector>
    
    void getVector( std::vector<std::string>& array ) 
    { 
    }
    
    int main () 
    {
    	std::string x[]={"Tom","Dick","Harry"};
    	std::vector<std::string> y(x,x+2);
    	getVector(y);
    }

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You might be able to do this after the next standard, but I don't remember the details.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    In C99 (I don't know about C++) you can create an anonymous data structure:
    Code:
    struct s_t {
        const char *s;
        int n;
    };
    
    void function(struct s_t *);
    
    int main(void) {
        function(& (struct s_t) {"hello", 6});
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by Daved
    You might be able to do this after the next standard, but I don't remember the details.
    Does that have to do with not being able to pass n (...) arguments of non-POD objects?

    It would be cool to get something like this working:

    Code:
    #include <cstdarg>
    #include <iostream>
    #include <vector>
    
    std::vector<std::string> getVector(int num, ...)
    {
    	va_list arguments; // a place to store the list of arguments
    	std::vector< std::string > vec(num); // a place to store the strings
    
    	va_start(arguments, num); // initializing arguments to store all values after num
    
    	for (int i = 0; i < num; i++) // loop until all numbers are added
    		vec[i] = va_arg(arguments, std::string); // adds the next value in argument list to vector
    
    	va_end(arguments); // cleans up the list
    
    	return vec; // returns the vector
    }
    
    int main()
    {
    	getVector(3, "this is a string", "and another", "and on and on");
    	
    	std::cin.get();
    }
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    How about:

    getVector(makeVector("string1", "string2", "string3"));

    Some languages, like javascript, not only have numeric literals(10, 20.2) and string literals("some text"), they also have array literals, function literals, and object literals, e.g.:
    Code:
    array literal: [10, 20, 30]
    function literal: function(){//display a greeting}
    object literal: {property1:10, property2:"hello", property3: function(){//display a greeting} }
    And, you can pass those literals around just like you pass around numeric literals. So, you can assign them to variables or pass them to functions, e.g.
    Code:
    my obj = {prop1:10, prop2:"hello", prop3: function(){//display a greeting} }
    
    or 
    
    functionCall([10, 20, 30]);
    Last edited by 7stud; 01-31-2006 at 08:12 PM.

  7. #7
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by 7stud
    How about:

    getVector(makeVector("string1", "string2", "string3"));
    I'm confused. In the OP example it makes the vector in getVector(), so makeVector() isn't necessary, and the OP also said concept-wise, not style (aka. get/make, whatever - can you get it to work). So 7stud, how would you get makeVector() to accept n arguments the way you have it there?

    Other languages may be able to do this, but thats just because they have that ability, but we're talking about C++.

    Darryl's way doesn't look too bad. I'd love to do it the way the OP has it though.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  8. #8
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    I don't think you can pass a physical array to a vector...

    but you can however, pass a reference to an array of size[n], and access it that way...
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    The closest idea I've come up with is that the OP might want to pass an array by value. And wrapping such a thing in a struct [C background] is one way to do this ugly thing. An example:
    Code:
    #include <stdio.h>
    
    struct type
    {
       int array[10];
    };
    
    void print(struct type object)
    {
       size_t i;
       for ( i = 0; i < sizeof object.array / sizeof *object.array; ++i )
       {
          printf("object.array[%d] = %d\n", (int)i, object.array[i]);
       }
    }
    
    void foo(struct type object)
    {
       size_t i;
       for ( i = 0; i < sizeof object.array / sizeof *object.array; ++i )
       {
          object.array[i] *= 10;
       }
       print(object);
    }
    
    int main()
    {
       struct type object = { {1,2,3,4,5,6,7,8,9,10} };
       foo(object);
       print(object);
       return 0;
    }
    
    /* my output
    object.array[0] = 10
    object.array[1] = 20
    object.array[2] = 30
    object.array[3] = 40
    object.array[4] = 50
    object.array[5] = 60
    object.array[6] = 70
    object.array[7] = 80
    object.array[8] = 90
    object.array[9] = 100
    object.array[0] = 1
    object.array[1] = 2
    object.array[2] = 3
    object.array[3] = 4
    object.array[4] = 5
    object.array[5] = 6
    object.array[6] = 7
    object.array[7] = 8
    object.array[8] = 9
    object.array[9] = 10
    */
    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.*

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Found the article that mentioned what I was talking about (thanks to Sang-drax):

    http://www.artima.com/cppsource/cpp0x.html

    Specifically the part about sequence constructors (near the end of the first page). But I'm guessing support for that sort of thing is still about 5 years away assuming they can come up with and standardize a solution.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Multidementional arrays
    By kingneb in forum C++ Programming
    Replies: 2
    Last Post: 10-25-2008, 03:24 PM
  2. More trouble passing arrays
    By Rad_Turnip in forum C Programming
    Replies: 2
    Last Post: 04-04-2006, 08:11 PM
  3. Passing 2 dimensional arrays to functions
    By homeyg in forum C++ Programming
    Replies: 7
    Last Post: 01-09-2005, 03:16 PM
  4. passing two dimensional arrays
    By Nova_Collision in forum C++ Programming
    Replies: 3
    Last Post: 02-04-2003, 01:47 PM
  5. passing arrays to functions
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 03-01-2002, 03:18 PM