0

I have a small program which when compiling throws me the following errors

error #2168: Operands of '+' have incompatible types 'struct agenda' and 'int'.

error #2113: Left operand of '.' has incompatible type 'int'.

error #2088: Lvalue required.

This is the code that I have done

#include <stdio.h>

struct agenda{
    int order, cellular;
    char name[30], last_name[30], street[30], city[30], mail[50];
}contact[10];

int main(void)
{
    struct agenda *ptrcontact;
    ptrcontact = &contact[0];

    (*ptrcontact+3).order = 3;

    printf("\n\n %d", (*ptrcontact).order);
    return 0;
}

because it throws these errors and how to fix them?

2
  • what is *ptrcontact? Its contact[0]. You should try something like: (ptrcontact + 3)->order = 3; Commented Feb 8, 2015 at 14:16
  • (*ptrcontact+3) gives you whatever ptrcontact points at (a struct agenda) and adds 3 (int) to that. * has higher precedence than +, so in order to get the third index, you have to either write: *(ptrcontact + 3) or ptrcontact[3]. Commented Feb 8, 2015 at 14:18

4 Answers 4

6

You need to change

(*ptrcontact+3).order = 3;

to

ptrcontact[3].order = 3;

or, at least,

(*(ptrcontact+3)).order = 3;

or,

(ptrcontact + 3)->order = 3;

Otherwise, as per the precedence rule, * has higher precedence over +, causing the error.

Just to add to that, ptrcontact is a pointer ( to struct agenda) and can be used as an operand to + operator.

OTOH, *ptrcontact is of type struct agenda and cannot be used as an operand to + operator.

Sign up to request clarification or add additional context in comments.

Comments

2

You are dereferencing the pointer which yields the struct and obviously you can't add anything to that. Dereference operator has the highest priority, you need to do it like this: (*(ptr + 3)).order or use the arrow instead of star dot: (ptr + 3) -> order

1 Comment

Man that really took me this long to type up
0

Your problem here is operations priority:

 (*ptrcontact+3).order = 3;

This derefers ptrcontract and then tries to add number to dereferred structure. Which gives you exact situation you report.

My recommendations:

Either avoid address ariphmetics in such cases. Operate array indexes.

int baseIndex = 0;
contact[baseIndex + 3].order = 3;

Or if you really have to do so, hide address arithmetic from outside:

(pcontact + 3)->order = 3;

And finally learn C language operations priority or, to do it once (but some C people don't like C++), C++ operations priority

5 Comments

This is a c question and the provided link will contain operators that are not even valid in the c language. en.cppreference.com is a c++ resource, not a c resource.
Exactly. But all valid operations priorities are as C programmer expects ;-). This is just to not learn them again when C++ starts.
I don't think every c programmer has to learn such an ugly language.
:-))) I agree with you somewhat. I've migrated to Java then Scala after C++11. Actually C++ is now too complex and too broad for single language - this results 'several languages in one'. But reality is - C programmers often learn C++ and (which makes the deal) then they return to C and become real programmers - after this they start to be language-agnostic. :-|
OK, I agreed C priorities at least should be referred first.
0

the error is in the lines (*ptrcontact+3).order = 3; and printf("\n\n %d", (*ptrcontact).order);. In this instructions use -> instead of . the errors will be solved.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.