Thread: problem with vector

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    98

    problem with vector

    how can i pass an element of vector<char*> to a function which takes an argument of char* ?

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Here's one quick, simple and very ugly example:
    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    void foo(char* s)
    {
         cout<<s;
    }
    int main( )
    {
        char* t1 = "Test1";
        char* t2 = "Test2";
        vector<char*> v;
        v.push_back(t1);
        v.push_back(t2);
        foo(v[0]);
    
       return 0;
    }

    - Micko
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    98
    that's what i tried to do, but it's not working, take a look here:
    Code:
    void func(vector<char*>* vec)
    {
         char str[256];
         //then i put some text in str
         vec->push_back(str);
         foo(vec[0]);
    }
    
    func(&vector);

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Note that in function func, vec is a pointer to a vector, not the actual vector. When you use the [] operator on the pointer, the compiler is assuming that you mean to do pointer addition. When you're calling foo, you're actually calling it with the vector object.

    You can fix this problem a few different ways.
    Code:
    Solution 1
    //Dereference the vector pointer, and then apply the [] operator
    void func(vector<char*>* vec)
    {
        //snip
         foo((*vec)[0]);  //Ugly, but it works.
    }
    However, you don't really need a pointer in this function. A reference would make this much nicer.
    Code:
    Solution 2
    //Use a reference to the object
    void func(vector<char*> &vec)
    {
        //snip
         vec.push_back(str);  //Use dot operator; no pointer
         foo(vec[0]);  //Works like you'd think
    }
    
    //To call
    func(vector);  //Pass the object, not the address
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Why are you using a pointer to a vector?

    The problem you are having is that the vector contains the pointer to the array's data. It doesn't contain the data itself.

    Instead of using vector<char*> you might want to use a vector<std::string> instead

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    98
    just to be sure:
    when i use a reference to the vector, there is no copy of the vector? it working with the original vector?

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    yep.

  8. #8
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Don't overlook Thantos's point, though. When you save that char* in the vector, remember that it will go out of scope once the function returns. You really should be using std::string instead.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  9. #9
    Registered User
    Join Date
    Apr 2005
    Posts
    98
    ok thanks all

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Also don't forget that a proper compiler should reject
    char *p = "Bla";
    String literals should only ever be assigned to const char *, because usually they're immutable.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Quote Originally Posted by CornedBee
    Also don't forget that a proper compiler should reject
    char *p = "Bla";
    String literals should only ever be assigned to const char *, because usually they're immutable.
    Is that true? I thought that was allowed for backwards compatibility with C?

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    technically it should reject it because it ignoring the const-ness of it. Though I have yet to use one that does.

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The standard allows it, but it is deprecated.

    It should not be rejected, but a warning would be appropriate, and you shouldn't be using it.

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Well, give GCC or MSVC the right compiler switches and they will reject it.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C programing
    By flame82 in forum C Programming
    Replies: 2
    Last Post: 05-07-2008, 02:35 PM
  2. Problem with pointer to vector objects
    By ryeguy in forum C++ Programming
    Replies: 5
    Last Post: 02-11-2008, 07:42 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. vector of pointers
    By gamer4life687 in forum C++ Programming
    Replies: 3
    Last Post: 09-26-2005, 10:49 PM
  5. Illegal vector index problem
    By Maiq in forum C++ Programming
    Replies: 5
    Last Post: 02-28-2003, 06:07 PM