Array pointers error

I'm trying to get the point for the array to return the value I enter, but it returns the memory location not the value. I tried using *y[2], but that gave me an error.


#include <iostream>

using namespace std;

int main()
{
int n;
cout << "Enter the number of elements:"<<endl;
cin >> n;

int x[n];
int *y;

y = x;

cin >> x[n];

cout << "hope this works " << y[2];

return 0;
}
you need to use the code brackets

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace std;

int main()
{
int n;
cout << "Enter the number of elements:"<<endl;
cin >> n;

int x[n];
int *y;

y = x;

cin >> x[n];

cout << "hope this works " << y[2];

return 0;
}


you are not using the pointer for dynamic memory.

1
2
3
4

int *array = new int [size];
cout << "enter the size of the array. ";
cin >> size;

Last edited on
Topic archived. No new replies allowed.