Please explain me this program

The program is not for homework im just reading c++ primer plus and its one of the listings it was quite a jump in difficulty! Explain every single line with a comment on the correct line so that it is easy for me to see. Please don't redirect me to a pointer tutorial...

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "stdafx.h"
#include <iostream>

const double * f1(const double ar[], int n);
const double * f2(const double [], int);
const double * f3(const double *, int);

int main()
{
	using namespace std;
	double av[3] = {1112.3, 1542.6, 2227.9};
	const double *(*p1)(const double *, int) = f1;
	const double *(*p2) (const double *, int) = f2;
	cout << "Using pointers to functions:\n";
	cout << " Address value\n";
	cout << (*p1)(av,3) << ": " << *(*p1) ( av, 3) << endl;
	cout << p2(av,3) << ": " << *p2(av,3) << endl;

	const double *(*pa[3])(const double *, int) = {f1, f2,f3};

	const double *(**pb)(const double *, int) = pa;
	cout << "\nUsing an array of pointers to functions:\n";
	cout << " Address Value\n";
	for (int i = 0; i < 3; i++)
		cout << pa[i](av,3) << ": " << *pa[i] (av,3) << endl;
	cout << "\nUsing a pointer to a pointer to a function:\n";
	cout << " Adress Value\n";
	for (int i = 0; i < 3 ; i++)
		cout << pb[i](av,3) << ": " << *pb[i](av,3) << endl;
	cout << "\nUsing pointers to an array of pointers:\n";
	cout << " Adress Value\n";
	const double *(*(*pc)[3])(const double *, int ) = &pa;
	cout << (*pc) [0] (av,3) << ": " << *(*pc) [0] (av,3) << endl;
	const double *(*(*pd)[3]) (const double *, int) = &pa;
	const double * pdb = (*pd) [1] (av,3);
	cout << pdb << ": " << *pdb << endl;
	cout << (*(*pd) [2] ) (av,3) << ": " << *(*(*pd)[2]) (av,3) << endl;
	cin.get();
	cin.get();
	return 0;
}



const double * f1(const double * ar, int n)
{
	return ar;
}
const double * f2(const double ar[], int n)
{
	return ar+1;
}
const double * f3(const double ar[], int n)
{
	return ar+2;
}



You must be joking. Post a specific line you don't understand and explain what you don't understand about it.
Line 1: stdafx.h is generated by a Visual Studio Wizard. You could just remove this line when compiling in a different environment.
Line 2: Defined standard C++ i/o methods

Line 4: Prototype for f1. f1 takes an array of doubles and an int as arguments and returns a pointer to a double.
Line 5: Prototype for f2. f1 takes an array of doubles and an int as arguments and returns a pointer to a double.
Line 5: Prototype for f3. f1 takes a pointer to a double and an int as arguments and returns a pointer to a double.

It should be noted that the actual functions do not match the prototypes! So really, this code isn't quite right. However, arrays in C and C++ are passed as address of the first element. So they all resolve to passing a pointer to a double for that first parameter. Tricky and seemingly wrong, but that's the way it is. Of course, there's a reason for everything.
http://www.cplusplus.com/forum/general/70081/#msg373940

Line 11: declare an array of 3 doubles.

Line 12: declare a pointer p1 that points to a fuinction that takes a pointer to a double and an int and returns a pointer to a double. Initialise it with the address of function f1.
Line 13: declare a pointer p2 that points to a fuinction that takes a pointer to a double and an int and returns a pointer to a double. Initialise it with the address of function f2.
Line 16: Call the function whose value is in p1 (that is f1) twice. Print the pointer returned on the first call, and dereference that pointer to print the double on the second call.
Line 17: Call the function whose value is in p2 (that is f2) twice. Print the pointer returned on the first call, and dereference that pointer to print the double on the second call.

Line 19: Declare an array of 3 pointers to functions that take a pointer to a double and an int and returns a pointer to a double. Initialise it with the address of functions f1, f2, f3.

It's all quite repetitive from here, so I'll stop unless you're stuck on a particular line.

You said this came from the C++ Primer? Wow, what was Stan Lipman thinking?
Stephen Prata to be specific, it is the sixth edition, just started and now he is throwing all this at me ^^. I found line 12-17 extremely confusing...
Have you covered pointers yet? Do know what a pointer is? Did the explanation help?

To be fair, the code is presented in a particularly oppresive way. It's not easy to read at all.
I have covered some of pointers. But generally I see them as confusing and most of the time useless. In his code he used the auto command but since I didn't understand pointers that well I decided to use the backwards compatible way which involved alot of * and () in hope of learning better how it worked but I guess it was just confusing. Ill just skip this code and hope I understand it with time :)

This is one of the exercises in chapter review, it was pretty fun and not too hard, hope it helped you understand my lvl with pointers. I think pointing at stuff is fun, but I find it a bit hard and confusing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "stdafx.h"
#include <iostream>
const int ArSize = 8;
void show_array(int * begin, int * end);
void array_to_n(int * begin, int * end,int n);
using namespace std;

int main()
{
	int arr[ArSize] = {10, 9, 8, 7, 6, 5, 4, 3};
	show_array(arr, &arr[ArSize]);
	int n;
	cout << "Make all the numbers in the array a different number: ";

	while(cin >> n)
	{
	array_to_n(arr, &arr[ArSize], n);
	show_array(arr, &arr[ArSize]);
	cout << "Now enter a new number: ";
	}
	cin.get();
	cin.get();
	return 0;
}
void show_array(int * begin, int * end)
{
	int *pt = begin;
	for(; pt != end;pt++)
		cout << pt << ": "<< *pt << endl;
}
void array_to_n(int * begin, int * end,int n)
{
	int *pt = begin;
	while(pt != end)
	{
		*pt = n;
		pt++;
	}
}


edit:
I do understand that passing a pointer to a function can be more efficient... I just don't understand how storing an original and a pointer can be better than just having the original! The pointer does also take space doesn't it?
Last edited on
I have covered some of pointers. But generally I see them as confusing and most of the time useless.
Ok, let's focus on pointers for a while. They're potentially confusing, but really useful.

First of all, do you understand that nearly all variables and all functions are placed in memory somewhere and that place in memory is call it's address?
Yes, I know about the address but don't know exactly the difference between *pt, pt and &pt. I know &pt should mean the address but in the program above just pt alone is the address and *pt is the value. Does this change depending on the situation?
Anyways here is a sample run of the program:

0052F9E8: 10
0052F9EC: 9
0052F9F0: 8
0052F9F4: 7
0052F9F8: 6
0052F9FC: 5
0052FA00: 4
0052FA04: 3
Make all the numbers in the array a different number: 3
0052F9E8: 3
0052F9EC: 3
0052F9F0: 3
0052F9F4: 3
0052F9F8: 3
0052F9FC: 3
0052FA00: 3
0052FA04: 3
Now enter a new number: q
The first part is to understand that variables and functions sit in memory somewhere and so have an address.

The next part is to understand that everthing in C and C++ has a type. The type defines what an object is and what you can do with it.

For example, a char is 1 byte. It's (usually) signed and has a range of -128 to 127 on an 8 bit byte system. You can do 8 bit arithmetic with it, encode an ASCII char with it ... all that wonderful stuff.

Another variable you have is a pointer. A pointer is a variable, so has an address and a type. You can assign a value to a pointer, dereference it (look at what it's pointing to), and do pointer arithmetic with it.

Do you get this so far?
Which program are you referring to, the one just above I can understand, because I made it :)
I was referring to pointers in general, but if you understand the principle, that's fine. I'll stop here.
Last edited on
I'll make new thread if I have a question in the future. Will probably be easier for anyone wondering about something similar to find, but thanks for explaining, hope you answer other questions I have on my journey :)
Topic archived. No new replies allowed.