Please explain this code to me

Hello!

I am studying by myself and I am so confused as to what these two codes does and how it works? Could someone explain this to me step by step in very simple terms?

I'm sorry for the inconvenience, the book is just a little too chock-filled w/ confusing language for me to really get it. Thank you in advance!



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
DonationList::DonationList(int num, double gifts[])
{
numDonations = num;
if (num > 0)
{

donations = new double[num];

arrPtr = new double*[num];

for (int count = 0; count < numDonations; count++)
{
donations[count] = gifts[count];
arrPtr[count] = &donations[count];
}

selectSort();
}
}




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void DonationList::selectSort()
{
int minIndex;
double *minElem;
for (int scan = 0; scan < (numDonations - 1); scan++)
{
minIndex = scan;
minElem = arrPtr[scan];
for(int index = scan + 1; index < numDonations; index++)
{
if (*(arrPtr[index]) < *minElem)
{
minElem = arrPtr[index];
minIndex = index;
}
}
arrPtr[minIndex] = arrPtr[scan];
arrPtr[scan] = minElem;
}
}
Last edited on
Well, the first one is a constructor for the class DonationList, the second one is a member function. I'm still a noob so that's all I can tell you with confidence and still make sense.

It's also hard to help you without knowing what you already know about them. Do you know about for loops? arrays? pointers?
I know about for loops, arrays and pointers yeah! Well. At the basic level, anyway.

I think all the meshing of them all is super confusing to me that's all. I'm just not sure what this program does . It's mostly with the second half though, like (*(arrPtr[index) < *minElem) , I have no clue what the heck that does.
Last edited on
* is the dereference operator. So for example *(arrPtr[index]) gets the value that is stored in the address pointed to by the pointer at the index 'index' of the array arrPtr.
I thought it was only able to get a value if there was an & involved somewhere and if not it was purely the address?

Like if it equated the pointer like this int *iptr = &hill and hill was defined before
int *iptr declares a new variable. The two *s are different operators.
The reference operator & gets the address of a variable.
1
2
3
int hill=4;
int *iptr=&hill;
std::cout<<"hill is "<<*iptr<<std::endl;

will produce the output
hill is 4

Basically, accessing iptr will get you the address while *iptr will get you the value stored in the address.
Last edited on
Ohhh I see!!

1
2
3
4
5
6
7
8
9
int main()
{
	double hill = 3.2;
	double *ptr = &hill;
	cout << *ptr << endl;
	cout << ptr;
	system("pause>nul");
	return 0;
}


So like in the code above, cout << ptr will print the address but *ptr will print the value right?

So with *(arrPtr[index]) , is it like some...inception-nized pointer? A pointer within a pointer that retrieves the value of the index? Am I getting it or am I far off?
Yes, though it's really just one pointer ^
arrPtr[index] gives you the address of the index
*(arrPtr[index]) gives you the value of the index
Got it!! Thank you so much for your patience, I really appreciate all the help :D !!
Topic archived. No new replies allowed.