I have an assignment question that says "Decide whether the syntax of the following statements is valid or invalid."

These are the statements:

a)  listData->next = ptr1->next ;
b)  listData->next = *(ptr2->next) ;
c)  *listData = ptr2 ;
d)  ptr2 = ptr1->next->info ;
e)  ptr1->info = ptr2->info ;
f)  ptr2 = ptr2->next->next ;

where next is the address of the next object, and info is the data.

I see no syntax errors... what don't I understand? :(

Dani AI

Generated

A short, practical checklist to resolve the kind of confusion in 's example (and the same point hinted at): every expression has a static type. To know whether an assignment is valid, determine the type produced by each subexpression (follow the pointer and dereference operators) and check whether the right-hand type is convertible to the left-hand type.

A minimal concrete sketch to make the types explicit:

struct Node {
    int info;
    Node* next;
};

Node* listData;
Node* ptr1;
Node* ptr2;

Notes that follow from that sketch:

  • X->member has the declared type of member. If member is Node*, X->member is a Node*.
  • *(P) where P is Node* yields an lvalue of type Node (an object), not a Node*.
  • Chaining -> pulls the member type step by step: ptr->next->info ends up with the info type (here int); ptr->next->next ends up Node*.

Practical checks and quick fixes:

  • Use decltype(...) to inspect the compile-time type of a subexpression:
    using T1 = decltype(listData->next);    // Node*
    using T2 = decltype(*(ptr2->next));     // Node&

    If the types do not match (for example one is Node* and the other is Node or int), the compiler will reject the assignment. Conversions are only allowed when C++ defines them (pointer-to-pointer conversions, arithmetic conversions, user-defined conversions, etc.). To fix a mismatch, either take the address of an object (&obj) to produce a pointer, or dereference a pointer (*ptr) to produce an object, as appropriate.

Caution: a well-formed type-level assignment can still crash at runtime if a pointer is null and then dereferenced. For reference on operator semantics see the C++ reference pages on indirection and member access and on assignment:

Member access operator ->
Assignment operator

Recommended Answers

All 7 Replies

Assignments can/should only be made between equivalent types. Follow the paths, there are some errors in there.

Can you assign the info member to a ptr? What to the couple of dereferencing actions do?

Pointer usage is one those areas where C/C++ give you lots of rope to shoot yourself in the foot.

Ok, so for example ptr->next can't be assigned ptr2->info ?

How about c)... listData is the starting pointer in the linked list. There's nothing wrong with this statement is there?

What does the * to to pointer listData? Does that give something that can be assigned a pointer value?

I don't think so? It's just a pointer to the first object in the linked list.

The objects contain a) data member (info) and b) address to next object (next).

I'm a little confused though, so I could be wrong?

Here are my updated answers.... I'm still not sure I quite understand this.

a.	OK
b.	Invalid; *(ptr2->next) is not a valid type for listData->next
c.	Invalid; listData cannot be de-referenced to accept a value of type pointer
d.	Invalid; ptr2 cannot be assigned a non-pointer value type
e.	OK
f.	OK

Unless someone else chimes in, I believe you have the right answers there.

Thanks for your help! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.