A function -> int_to_coordinate(int, int id_array[5][5]) returns a pointer to an object in an array, the object can be accessed through the pointer in this function, but cannot be accessed by any function that uses the returned pointer. By "cannot be accessed" I mean the compiler catches an unhandled exception and gives that "Access violation..." error.
class Grid_info
{
public:
Grid_info::Grid_info();
Grid_info::Grid_info(int);
int item[5][5];
int direction[5][5];
private:
//Other functions...
vector<int>sample_vct;
bool vectors_to_arrays(vector<int> *v, char); //calls int_to_coordinate(int, int[5][5]) and uses the pointer returned
int* int_to_coordinate(int, int[5][5]); //returns pointer to object in array (item[5][5])
};
bool Grid_info::vectors_to_arrays(vector<int> *v, char frog_bug)
{
int fb=-1;
if (frog_bug=='f') fb=1;
elseif (frog_bug=='b') fb=2;
else sys_error(...);
for (int a=0; (unsigned)a<(v->size()); a++)
{
int* position=int_to_coordinate(v->at(a),item); //passes an integer and an array _ assigns the returned pointer to int* position
*position=fb; //>>> gives "Access violation..." error, tested diff functions, can't access the object pointed by the pointer returned by int_to_coordinate(int, ...)
}
returntrue;
}
int* Grid_info::int_to_coordinate(int pos, int id_array[5][5]) //returns a pointer to one of the objects in the array passed as argument
{
int* pos_p=0;
int counter=1;
for(int a=0; a<5; a++)
{
for(int b=0; b<5; b++)
{
if (counter==pos)
{
pos_p=&id_array[a][b];
//the object pointed by the pointer pos_p can be accessed at this point (*pos_p=int)_tested
if (pos_p=0) sys_error(...);
return pos_p;
}
counter++;
}
}
sys_error(...);
}
tried it, no difference... pos_p actually gets the correct address, i can access it from this function perfectly fine, i just can't access it from any other function that uses the pointer (pos_p) returned by int* Grid_info::int_to_coordinate(...)