Pointer Notation

I'm trying to figure out a program that uses pointer notation to display numbers in an array. I have to use the pointer notation *arrayname.

This is what I have to do: Modify the show() function to alter the address in rates. Use the expression *rates rather than *(rates + i) to retrieve the correct element.

This is what I have so far:
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
#include <iostream>
#include <iomanip>

using namespace std;


void show(float[]);


int main()
{
	float rates[9] = {6.5, 7.2, 7.5, 8.3, 8.6, 9.4, 9.6, 9.8, 10.0}; //declaring array


	cout << "The numbers are: " << endl;

	
	
	show(rates); //calls function
	


	getchar();
	getchar();
	return 0;

}

void show(float rates[])
{
	int i;
	
	for (i=0;i<9;i++) //uses a for loop along with pointer notation to print the number for each element
	{
		rates[i] = *(rates + i); // I know this doesnt do much
		cout << rates[i] << "  ";
	}

	return;
}
Good luck with your homework. (http://www.cplusplus.com/forum/beginner/1/)

Ciao, Imi.
rates[i] and *(rates + i) are equivalent expressions.

The first uses the index operator and the second uses pointer arithmetic (and a dereference).

You can pass either to cout and get the same result.
Last edited on
Topic archived. No new replies allowed.