Reputation: 2520
I have just started learning c++ and I am stuck over here. I made a structure
...
struct student{
string name;
int roll;
};
...
And then, I called it globally. student s1[20];
Inside my main function,
I did something like this
...
int count=0;
student *p;
p=s1;
cout<<"Enter the Number of records, you wish to consider.";
cin>>arraySize;
p = new student[arraySize];
while (count!=arraySize) {
cout<<"Enter name.";
cin>>*(p+count)->name; //Error comes over here
}
...
The error message, I get is Indirection requires pointer operand
. Can some one Please help me out with this ??
Upvotes: 1
Views: 45
Reputation: 117926
You could either use pointer arithmetic as you are doing
(p+count)->name
or more canonically use []
to index into the array
p[count].name
The reason you are getting an error is because you are combining *
which dereferences your pointer with ->
which also dereferences your pointer. You could just do one or the other
(p+count)->name
(*(p+count)).name
Though as I said for readability I would prefer using index notation []
Upvotes: 5