array as an argument

This is a discussion on array as an argument within the C++ Programming forums, part of the General Programming Boards category; Say in main I have: PHP Code: int nums [ 5 ]; set_numbers ( nums );  and I have a function ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    137

    array as an argument

    Say in main I have:
    PHP Code:
    int nums[5];

    set_numbers(nums); 
    and I have a function called set_numbers() where I want to set the values for the numbers in the array:
    PHP Code:
    void set_numbers(int n[])
    {
         
    n[0] = 1;
         
    n[1] = 2;
         
    n[2] = 3;
         
    n[3] = 4;
         
    n[5] = 5;

    that code works, but everywhere I read that you have to pass arrays by reference, and when I try to do that I get an error. Whats wrong with doing it how I'm doing it now, and how do I pass it by reference instead?

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,805
    Nothing thats the perfect way to do it.
    Woop?

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    hmm.. ok well.. then can someone tell me how to make the iint nums[5] array in main the default array to use?

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    arrays are always passed by reference. What do u mean by default array?

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    like if i were passing an int i could do
    PHP Code:
    void set_numbers(int j 1){} 
    i want to set the default value for int n[] to the array nums[] in the main function. can anyone tell me how to do that.

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,805
    No way really to do that. A smart person always passes the size of the array as an parameter.
    Woop?

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    well then how would i get around that? I have a dll and I have an exe that tell the dll to create an instance of a class and i have to save that instance inside a vector. So don't I have to use a default vector?

  8. #8
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,805
    Huh? If you are using a vector then use .size().
    Woop?

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    no. i'm not talking about the size. i'm talking about when i want to insert into a vector. the vector is outside of the function so how do i pass that vector to the function so i can insert the new instance in it?

  10. #10
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    judging from your original post maybe you mean something like this??
    Code:
    #include <iostream>
    void set_numbers(int * ptr);
    int main()
    {
    	int * ptr = new int[5];
    	set_numbers(ptr);
    	std::cout << ptr[2];
    	delete ptr;
    }
    void set_numbers(int * ptr)
    {
    	ptr[2] = 11;
    }

  11. #11
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,805
    Or if you want to pass the vector
    Code:
    void function(std::vector<type> &toPass);
    Woop?

  12. #12
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    hmm. i shouldn't have said anything about the main function because this question isn't literal. its in a dll and there is no main function. i have a vector that i create at the start and whenever the exe calls a function i want it to add an instance to that vector. prog-bman is that what you're suggestion will do?

  13. #13
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,805
    Yes that will pass a vector from a function to another function and modify the contents within that vector.
    Woop?

  14. #14
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    i have a question though, what if i can't pass the vector from the exe that is calling the function?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 09-22-2006, 05:21 PM
  2. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM
  4. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

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