0

Hello ,

I have this:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>

using namespace std;

typedef struct 
{
    float x;
    float y;
    Point *next;

} Point;

int main() 
{

    Point *thePoints = new Point[2];

    thePoints[0].x = 2;
    thePoints[0].y = 3;
    thePoints[1].x = 4;
    thePoints[1].y = 5;

    (thePoints[0])->next= 11;
    cout << (thePoints[0]->next <<endl;

    return 0;

}

but at the last 2 lines I can't access (thePoints[0])->next. (base operand of ‘->’ has non-pointer type ‘Point’)

I can't undestand why!

3
Contributors
11
Replies
99
Views
10 Months
Discussion Span
Last Post by glao
Featured Replies
  • I'm not sure where this `int*` idea is coming from. If you are creating a linked list of Points, seems like next should be a `Point*`. Linked lists are generally like this... struct X { // some data members X* next; // points to next element in list. }; So … Read More

0

Here's the thing. You didn't reveal what your code was supposed to do. I would be guessing what all that code was supposed and can't seem to find a data type Point in line 12. Maybe you left out more code or just need to reveal the software design specification.

Edited by rproffitt

0

The code is what you can see, nothing more.I am just experimenting.I want just to fill the next pointer with value 11 and print it.

0

Then let's call this a failed experiment. That is, when we write code we have a design or result in mind. If you just want to print the value 11, you can take a hello world and change the string to 11.

There's a lot more wrong with your code than lines 12 and 27. For example, C++ line 27 even if it worked would not print "11" but would output, possibly a Vertical Tab character. Don't believe it? Simplify it to setting a value to 11 and printing that value.

Can you share how you got this far? It's odd to see all those includes and you not use them.

0

Ok, let's say I have this code: ( the includes were left over )

#include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;

typedef struct Point
{
    float x;
    float y;
    int *next;

} Point;

int main() 
{

    Point *thePoints = new Point[2];

    thePoints[0].x = 2;
    thePoints[0].y = 3;
    thePoints[1].x = 4;
    thePoints[1].y = 5;

    (thePoints[0])->next= 11;
    cout << (thePoints[0])->next <<endl;

return 0;

}

Why it doesn't compile and gives me:
base operand of ‘->’ has non-pointer type ‘Point’

Then, if my struct is:

typedef struct Point
{
    float x;
    float y;
    Point *next;

} Point;

and try:

(thePoints[0].x)->next= 11;

why it gives me : base operand of ‘->’ is not a pointer

I hope it is clear.I just want to insert the value 11.

Thanks

Edited by glao

0

You first have next defined as a Point*. 11 is not a Point*. Then you redefined it as an int*. 11 is not an integer, not a pointer to an integer. So you have next defined as a pointer, not an integer, in both definitions. That is your error.

I hope it is clear.

It is clear why you got the error. What you are trying to do is NOT clear, so I can't tell you how to fix it.

I just want to insert the value 11.

Insert the value 11 into WHAT? Is this an attempt to make a linked list?

0

Hmm..Yes, you are right , I am confused..

I solved the first problem (using int) like:

int a = 11;
thePoints[0].next = &a;
cout << *(thePoints[0].next) <<endl;

and it works ok.But I would expect it to work using ->:

thePoints[0]->next = &a;
or
thePoints[0]->x = 2;

and it doesn't.

As for the second case, yes,just trying to use a linked list.

So, I want to initialize thePoints[0].x = 2; thePoints[0].y = 3
and copy that stucture to the next.

Something like : thePoints[1]->next = thePoints[0];

Thank you.

0

Ok, regarding the first problem , I did:

   Point *thePoints = new Point[2];

   thePoints[0].x = 2;
    thePoints[0].y = 3;

    int a = 11;
    (&thePoints[0])->next = &a;

cout << *(&thePoints[0])->next <<endl;

and it works fine!
I confused that the thePoints[0] was not a pointer but a value.

Regarding the second problem (linked list) , I tried:

   Point *thePoints = new Point[2];

    thePoints[0].x = 2;
    thePoints[0].y = 3;

    (&thePoints[1])->next = &thePoints[0];

cout << thePoints[1].x <<endl;

but I am receiving a value of 0 instead of 2 in the cout statement.

Edited by glao

0

but I am receiving a value of 0 instead of 2 in the cout statement.

You took out the code assigning thePoints[1] values. Stick it back in.

#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

struct Point
{
    float x;
    float y;
    Point* next;
};

int main() 
{
    Point *thePoints = new Point[2];
    thePoints[0].x = 2;
    thePoints[0].y = 3;
    thePoints[1].x = 4;
    thePoints[1].y = 5;
    (&thePoints[1])->next = &thePoints[0];
    cout << thePoints[1].x <<endl;
    cout << thePoints[1].next->x << endl;
    return 0;
}

Note the new line I added just before the return 0. You should get a printout of 4, then 2.

I am noting some confusion between the dot operator and the arrow pointer. Use the dot when you have a Point object. Use the arrow operator when you have a Point* object. See code below. You should get 8 9 displayed twice.

    Point pt1;
    Point* pt2 = &pt1;
    pt1.x = 8;
    pt2->y = 9;
    cout << pt1.x << " " << pt1.y << endl;
    cout << pt2->x << " " << pt2->y << endl;

Thus you can change to (&thePoints[1])->next = &thePoints[0]; to thePoints[1].next = &thePoints[0]; They do the same thing.

0

Ok, thanks!
This cout << thePoints[1].next->x << endl; works!

My confusion is not only on thePoint thePoints or Point *thePoints
but also the presence of a pointer inside the structure int *next or Point *next.
and how to access this field with each case.

Thanks again!

2

I'm not sure where this int* idea is coming from. If you are creating a linked list of Points, seems like next should be a Point*.

Linked lists are generally like this...

struct X
{
    // some data members
    X* next; // points to next element in list.
};

So next will be a pointer to the struct type. In your case, Point*.

thePoints is an array. Its type isPoint*. It points to the first element of the array, element 0.

thePoints[0] and thePoints[1] are type Point, not type Point*. The brackets [] dereference it. You use the dot after the brackets. You use the arrow after a pointer. The code below is overly-complicated, but it hopefully gets the point across on how you can do the exact same thing different ways.

Point* ptr = thePoints;  // ptr points to point 0.
Point* ptr2 = &thePoints[0]; // different way of doing the above.  ptr and ptr2 point to the same thing
ptr->x = 5;                  // ptr is a pointer.  USe arrow.
thePoints[0].y = 6;          // point 0 is now (5,6).  Use the dot after the bracket.
ptr->next = &thePoints[1];   // the next point is point 1.
ptr2->next->x = 7;           // note.  Here I used TWO arrows since ptr and next are pointers.
thePoints[1].y = 8;          // point 1 is now (7, 8).  Here I used the dot.
cout << thePoints[0].x << " " << thePoints[0].y << endl; // 5 6
cout << thePoints[1].x << " " << thePoints[1].y << endl; // 7 8
Votes + Comments
Cristal clear!
0

Thanks a lot for the example.I can understand it , ok .
When I used int *next it wasn't my intent to use a linked list.
It was just a field in the Point structure.I just wanted to test how to access it.

This question has already been answered. 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.