Title says it all, basically I'm just trying to print the pointer *p to display all the values in that array.

#include <iostream> 
#include <string> 
//#include <stdlib>
using namespace std;
void extend(double *p, double *q, double *a, int c);

int main()
	const int CONST = 10;
	double price[CONST] = { 14.89, 13.21, 16.55, 18.62, 9.47, 6.58, 18.32, 12.15, 3.98 };
	double quantity[CONST] = { 4, 8.5, 6, 7.35, 9, 15.3, 3, 5.4, 2.9, 4.8 };
	double amount[CONST];

//	double *p = &price[0];
	extend(price, quantity, amount, CONST);


void extend(double *p, double *q, double *a, int c)
	double p1 = *p++;
	cout << p1;
	for(int i = 1; i < 10; i++)
		cout << p1[i];


Edited by peq: n/a

7 Years
Discussion Span
Last Post by peq

And now we have to guess what the problem is, right? Why don't you help us out and tell us what you're having problems with, ok? Makes things a lot easier for the people willing to spend time trying to help you.


error C2109: subscript requires array or pointer type, is the error message I'm getting


Yes, so how should you go about 'debugging' this?
First, look at the line where you got this error:

cout << p1[i];

Here, you say 'print the ith element of the array p1'. Now lets look at how p1 is declared:

double p1 = *p++;

It's a double!, not an array of doubles.

You should simply discard 'p1' and use 'p' everywhere:

cout << p[i];

Got it, completely flew over my head, many thanks.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.