Thread: So this is how you pass arrays to functions!

  1. #1
    Shadow12345
    Guest

    So this is how you pass arrays to functions!

    You just use pointers to pass arrays of any size to a function/method?

    something I found (I did not write this)
    Code:
    	// This takes a list of vertices and the vertex count to determine if the camera's
    	// shere has collided with them.
    	void CheckCameraCollision(CVector3 *pVertices, int numOfVerts);
    	//USE A POINTER TO PASS IN AN ARRAY OF AN UNSPECIFIED SIZE
    Do you always have to declare the array using pointers in order to pass the array to a function

    For example the array that is actually passed to this function when it is called is declared as:
    CVector3 *g_vWorld=NULL;
    Last edited by Shadow12345; 11-11-2002 at 09:55 AM.

  2. #2
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    No, you can pass it without it being a pointer, but you would be makeing a copy of the array. Just like when you pass a non-pointer variable, you are making a local copy of that variable in your function. (unless you pass a reference)

    Also, if the array has the ability to provide its element count to you, then there is no need to pass the elements as a seperate parameter.
    Best Regards,

    Bonkey

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Nope. Since the argument is a pointer to the type n, it's not neccessary an array. You can pass a single variable as well as a statically or dynamically allocated array:
    Code:
    void MyFunc(int* MyPointer)
    {
       ...
    }
    
    int main()
    {
       int Var1;
       int Var2[32];
       int* Var3 = new int[1024];
    
       MyFunc(&Var1);
       MyFunc(Var2);
       MyFunc(Var3);
    
       delete[] Var3;
       return 0;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Shadow12345
    Guest
    I don't see why you don't pass the memory address of the arrays to the function.
    Last edited by Shadow12345; 11-11-2002 at 02:30 PM.

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Shadow12345
    I don't see why you don't pass the memory address of the arrays to the function.
    You mean in my example? That's because an array is basically a pointer to a chunk of memory, thus when you pass the "array" you actually pass a pointer to it.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. functions using arrays
    By trprince in forum C Programming
    Replies: 30
    Last Post: 11-17-2007, 06:10 PM
  2. arrays and functions
    By dantestwin in forum C++ Programming
    Replies: 9
    Last Post: 07-08-2004, 03:30 PM
  3. using pointers to pass multi-d arrays
    By aaronc in forum C Programming
    Replies: 1
    Last Post: 04-27-2004, 03:07 AM
  4. Help Understanding Passing Arrays To Functions
    By jrahhali in forum C++ Programming
    Replies: 7
    Last Post: 04-10-2004, 02:57 PM
  5. How can I pass int arrays from C++ to C#?
    By Vince in forum C++ Programming
    Replies: 0
    Last Post: 11-11-2002, 02:33 PM