passing dynamic array to function

This is a discussion on passing dynamic array to function within the C++ Programming forums, part of the General Programming Boards category; HI, I'm trying to create a dynamic array in one function and then passing it to another function. However I ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    84

    passing dynamic array to function

    HI,

    I'm trying to create a dynamic array in one function and then passing it to another function. However I am not able to get it to work ? can someone please tell me how to do this properly ?

    Heres my code -

    Code:
           void CreateCars()
          {
              Cars* cars = new SportsCar[50];
    	  for(int i = 0; i < 50; ++i)
    	 	cars[i].SetSpeed(rand()%100);
    	  carHandler = new CarHandler(cars,50);
           }

    Code:
    CarHandler::CarHandler(Car* car_array,int numCars)
    {
    	this->car_array = car_array;
            this->numCars = numCars;
    }
    Can someone tell me how to do this properly ?
    Thank you!

  2. #2
    Registered User
    Join Date
    Nov 2007
    Posts
    19
    CarHandler constructor receives a pointer to a Car but you are passing a pointer to a Cars

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    84
    sorry, that was a typo.
    Code:
    CarHandler::CarHandler(Cars* car_array,int numCars)
    {
    	this->car_array = car_array;
            this->numCars = numCars;
    }
    I have cars in both places, but it doesnt work.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,022
    Can you specify more exactly how it does not work? What you have posted should work fine, though I urge you to use std::vector instead.
    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.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    84
    Yes, It worked! The problem was further on in the code.

    Thanks everyone!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  3. Passing a double array from main to another function
    By Loctan in forum C++ Programming
    Replies: 2
    Last Post: 01-04-2005, 05:19 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. passing array of structures to function
    By bvnorth in forum C Programming
    Replies: 3
    Last Post: 08-22-2003, 08:15 AM

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