question about passing a pointer to an array of objects into a function


Folks, I am new to C++ and learning how to use objects. I defined a object called credit and then created an array of credits. But when I pass this array to a function the function does not seem to recognize its inputs. I am able to print the contents of the array outside the function.

Is this an error resulting from an incorrect definition of a copy constructor?

I am going to include just those snippets of the code here that I think are relevant. Hopefully this will be enough.

Would really appreciate the help.

Main program
int main()
{
credit *creditlist; //credit is an object defined before hand


// Here the get_credits function ouputs a pointer to an array of credits
creditlist = get_credits("creditdata.txt",&ncredits); //This works

//I check to see if the creditlist object contains what it should contain
//Output is sensible
for(i=1;i<= ncredits; i++)
{
for(j=1;j<=NFACTORS;j++)
{
cout << creditlist[i].betafactor[j] << endl;
}
}

//Next I pass creditlist to another function which I include below

get_default_times(creditlist);

//Here is the function and a small portion of the code which is tripping up
int get_default_times(credit *creditlist)
{
cout << creditlist[1].betafactor[1] << endl; //THIS GIVES ME AN ERROR
return 0;
}


This is the error messsage I get:

Unhandled exception in Simulation2.exe: 0xC0000005: Access Violation
[code] "Please use code tags" [/code]
Array index goes from 0 to n-1
¿How are you creating the array in get_credits() function?
Hi,
Thanks for getting back. I am new to this. Can you explain what you mean by code tags?

To answer your questions:
In the code above, the creditlist array goes from 1 to ncredits. I also print out the elements as in the code above.


Here is how I create the array in get_credits():

credit *get_credits(char *filename, int *ncredits)
{
creditdata = read(filename); // creditdata is a matrix object that reads in data
creditlist = new credit[*ncredits]; //Creates an array for the credits from 0 to ncredits-1
creditlist--; //for unit offset;
OTHER CODE FOLLOWS which assigns values to creditlist
return creditlist;
}
Last edited on
Hi,

I actually found what I was doing wrong. Had to do with not using the right variable for indexing the array. I was using 'j' instead of 'jcredits'. Thanks so much for getting back to me.
Topic archived. No new replies allowed.