Thread: how to declare and pass the vector pointer in c++

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    134

    how to declare and pass the vector pointer in c++

    Give me an instance of declaring a vector pointer and passing it to a function/

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What have you tried? Why are you not passing the vector by (const) reference?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    134

    Can you elaborate it a bit?

    I am a beginner so I don't understand very much technical words, kindly elaborate, actually i want to send a vector to a function and get the value of the vector from the function
    I can understand that this can be achieved by return of the function or sending the pointer of the vector and writing the same there in function.

    what does it mean by passing the vector by (const) reference?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by kapil1089thekin
    I am a beginner so I don't understand very much technical words, kindly elaborate, actually i want to send a vector to a function and get the value of the vector from the function
    I can understand that this can be achieved by return of the function or sending the pointer of the vector and writing the same there in function.
    I suggest that you read a book that will explain these basics to you in a more structured fashion, complete with code examples. For example, you could work through Accelerated C++ by Koenig and Moo.

    Otherwise, we will essentially be writing a tutorial specially for you.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    134
    I am consulting the book Complete C++ reference by Herbert Schildt, not getting exactly what I am posting here, more then that I am intermediate not exactly beginner, but trying to learn techniques and when I don't find it there I post them here.

    Kindly help if possible.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by kapil1089thekin
    I am consulting the book Complete C++ reference by Herbert Schildt
    I would approach with caution any book by Herbert Schildt as books by that author are notorious for factual inaccuracy.

    Have you not seen a code example similiar to:
    Code:
    void add123(std::vector<int>& numbers)
    {
        numbers.push_back(123);
    }
    In the above function, numbers is a reference parameter, hence a call such as:
    Code:
    std::vector<int> v;
    add123(v);
    will cause v to have 123 pushed to its back.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    134

    C++ is too much diffrent from c

    In C to send the parameter by reference we had to declare the parameter as pointer, but in C++ declaring it by name but the writing the function in the format u said works, can you explain why is it so? what is going the story behind?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Within the bounds of the language, when you do
    add123(v), then numbers becomes an alias for v. That is, they refer to the same object, with numbers being a different name for v.
    That is really all to it.
    You don't really need to care about how it works behind the scenes, because it's not specified by the standard and is irrelevant.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to pass a matrix/array from main to a function
    By Inukami in forum C Programming
    Replies: 7
    Last Post: 12-09-2009, 09:03 PM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  3. Replies: 2
    Last Post: 05-04-2003, 05:30 AM
  4. So this is how you pass arrays to functions!
    By Shadow12345 in forum C++ Programming
    Replies: 4
    Last Post: 11-11-2002, 03:08 PM
  5. I want to pass in a pointer to a vector of type Triangle*
    By Shadow12345 in forum C++ Programming
    Replies: 1
    Last Post: 10-13-2002, 08:26 AM