r/programminghelp • • Feb 14 '24

C pointers in C

so i'm learning C right now and i have a question about the use of pointers in arithmetic.
so when i declare ptr1 and ptr2 as pointers, and then i write *ptr2=*(ptr2)+ptr1 it works fine.

but if i write *ptr2+=ptr1 which is almost the same as before i get an error. can someone help me understand the += because it seem the problem is there

1 Upvotes

5 comments sorted by

View all comments

Show parent comment

2

u/Furry_69 Feb 14 '24

In your calculation, ptr1 is not dereferenced. Of course, adding a pointer to an int doesn't make any sense, but internally they're both numbers, so the compiler will just add them anyways and emit a warning.

1

u/thegodfather444 Feb 14 '24

If i does ptr2=(ptr2)+ptr1 it works but i i do it with += its not. Is the problem lies in the += operation?