Thread: changing value in an array by using a function

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    6

    changing value in an array by using a function

    I have some problems with this code. I am trying to change the values of the selectedArrays below by calling this function. But it doesn't work. Can anyone see why?
    I know that the Stores,Suppliers and Plants Arrays are containing values, but I cannot get the selectedArrays to point at them.

    Code:
    void createSelectedArrays(int* binaryDecisionArray,int** selectedStores,int** selectedPlants,int** selectedSuppliers,Environment* e){
    	int *Stores,*Plants,*Suppliers;
    
    /*
    Here I fill the Stores, Suppliers and the Plants array with values. Then I want the selectedArrays to contain the values of Stores,Suppliers and Plants. But it want work .. :(
    */
    
    //I fill them like this
    for(i=suppliersMin; i<suppliersMax;i++){
    		if(binaryDecisionArray[i]==1){
    			Suppliers[supplierLocation]=i;
    			supplierLocation++;
    		}
    	}
    
    	selectedSuppliers = &Suppliers;
    	selectedPlants    = &Plants;
    	selectedStores    = &Stores;
    	
    }
    This is how I call the function in an other function.

    Code:
    int *selectedStores,*selectedPlants,*selectedSuppliers;
    
    createSelectedArrays(binaryDecisionArray,&selectedStores,&selectedPlants,&selectedSuppliers,e);

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Try this:
    Code:
    *selectedSuppliers = Suppliers;
    *selectedPlants    = Plants;
    *selectedStores    = Stores;

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >void createSelectedArrays(int* binaryDecisionArray,int** selectedStores,int** selectedPlants,int** selectedSuppliers,Environment* e){

    Is there some reason for the arrays to be dynamic? If not, it would be easier to pass arrays and load them directly:
    void createSelectedArrays(int* binaryDecisionArray,int selectedStores[],int selectedPlants[],int selectedSuppliers[],Environment* e){

    Otherwise (unless you've only posted part of the function) you must allocate some memory for each array, then use The Dog's code.

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    6
    Thanx! Now at least that works.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM