I'm currently stumped on a homework problem involving a function that iterates through the first column of an array of 2 columns to find the minimum value, then 开发者_开发问答returning a pointer to the element with the minimum value. That part isn't too bad, but my problem is this:
cout<<"The minimum travel time is "<<*minimumTravelTime(travelTime)<<endl; //Prints minimum travel time
//FIX THIS PART
methodDeterminator = (*minimumTravelTime(travelTime))++;
cout<<"THE CASE IS "<<methodDeterminator<<endl;
//FIX THIS PART
switch(methodDeterminator)
{
case 0: transportMethod = "transporter";
break;
case 1: transportMethod = "stargate";
break;
case 2: transportMethod = "Alcubierre drive";
break;
case 3: transportMethod = "hyperdrive";
break;
case 4: transportMethod = "TARDIS";
break;
default: cout<<"Error. Could not find transport mode."<<endl;
}
cout<<"Travel by "<<transportMethod<<" will take "<<*minimumTravelTime(travelTime)<<" months."<<endl; //Prints travel method and time
The function to find minimum time is here
int *minimumTravelTime(int travelArray[][2])
{
int *pMinimum; //Pointer for minimum time
int currentMin, pastMin; //Holding places for finding minimum travel time
for(int r = 0; r < 5; r++) //5 rows
{
for(int c = 0; c < 2; c++) //2 columns
{
if(c == 0) //If first column
{
if(r == 0) //If first row
{
currentMin = travelArray[r][c]; //First row value stored as currentMin
}
else
{
pastMin = currentMin; //Set previously determined currentMin as pastMin
currentMin = travelArray[r][c]; //Get current value
if(currentMin > pastMin) //If current value is greater than pastMin
{
currentMin = pastMin; //Set currentMin to be pastMin
}
}
}
}
}
pMinimum = ¤tMin; //Points to memory location of currentMin
return pMinimum; //Return address of minimum value
}
The part labeled "//FIX THIS PART" is where I'm having trouble. I want it to increment the memory address that the function returns BEFORE dereferencing it (so I can get the value in the second column of the array, which will correspond to the cases in the switch statement). I cannot remove the * in the function name in the implementation of the function, nor can I remove the inner parentheses, otherwise it will give me an error "Expression is a non-modifiable lvalue."
Thank you for any help. I've been working at this problem for hours trying to figure it out. I also couldn't find any example code of specifically what I'm trying to do (increment a pointer before dereferencing it, and the pointer is returned by a function).
There seem to be multiple problems with the code that you have given.
for(int c = 0; c < 2; c++) //2 columns
{
if(c == 0) //If first column
This part makes sure that your loop will never check the second column, which is wrong.
The logic of incrementing the returned pointer value will not work as the returned value is not a pointer inside array, but a pointer to local variable (problems already pointed by Nawaz). Moreover, you have already located the minimum value from the second column inside the function minimumTravelTime, so there is no need to further processing.
If you still want to increment the returned pointer before dereferencing it, you will have to do pre-increment. However, the returned value should be first collected into a local pointer and then pre-incremented, otherwise you will get the 'lvalue' error.
methodDeterminator = minimumTravelTime(travelTime)[1];
or equivalently
methodDeterminator = *(minimumTravelTime(travelTime) + 1);
However, as explained by Nawaz, you are not returning pointer to the column. You need to set
pMinimum = travelArray[r];
where you set currentMin
and just return that from minimumTravelTime
function. Than the use of [1]
should even be clear—the function returns the array with one index and you add the second index.
However, this being C++, you should be using std::map
in the first place. C++ provides many high lever concepts to avoid manipulating pointers and you should use them to keep your code maintainable and to avoid silly mistakes (you have at least 3 serious problems in the code that would be caught by the compiler if you used appropriate constructs).
pMinimum = ¤tMin; //Points to memory location of currentMin
return pMinimum; //Return address of minimum value
currentMin
is a local variable, and so you're returning a pointer to the local variable. This is the problem, as the local variable doesn't exist after the function returns, but you're still trying to access it on the calling site. That wouldn't produce desired result.
Better change the return type of the function and then return currentMin
by value as,
int minimumTravelTime(int travelArray[][2]) //changed the return type!
{
//your code
return currentMin;
}
And call it as,
int minTravelTime = minimumTravelTime(travelTime);
EDIT:
If you're instructed to return a pointer from the function, then you can do this:
return new int(currentMin); //allocate memory, initialize it, and return it!
Note the syntax it's new int(currentMin)
, not new int[currentMin]
. There is a difference between these two syntax!
And on the calling site:
int *minTravelTime = minimumTravelTime(travelTime);
//use minTravelTime
delete minTravelTime; //delete the memory once you're done!
I want it to increment the memory address that the function returns BEFORE dereferencing it.
The pointer returned by minimumTravelTime()
is not an l-value (it doesn't have an address in memory as far as your program is concerned), which means you're not allowed to modify its value as you would if the pointer were declared as a variable.
What you can do is add a value to the returned pointer and dereference the result, like this:
methodDeterminator = *(minimumTravelTime(travelTime) + 1);
... or, better yet, use array notation to accomplish the same thing, like this:
methodDeterminator = minimumTravelTime(travelTime)[1];
You could also do something like this:
int *p = minimumTravelTime();
methodDeterminator = *(++p); // increment first, then dereference
... but there's no good reason to do that unless you wish to use the pointer elsewhere.
精彩评论