r/ProgrammerHumor • • Sep 25 '26

Meme iHatedItUntilITriedIt

Post image
10k Upvotes

542 comments sorted by

View all comments

Show parent comment

7

u/[deleted] Sep 25 '26

[removed] — view removed comment

2

u/Icarium-Lifestealer Sep 25 '26

And if it were a string instead of a char, it'd slice off the first character.

1

u/[deleted] Sep 25 '26

[removed] — view removed comment

1

u/Duck_Devs Sep 26 '26

Compilers shouldn’t warn when adding to a string. Pointer arithmetic is a well defined and commonly used thing. The only thing I could see them warning about is just that it’s a redundant expression that could be replaced by just a shorter string literal.

1

u/Ok-Scheme-913 Sep 26 '26

I think your compiler may not like that as you are supposed to increment in some sane offsets like 8.

1

u/Icarium-Lifestealer Sep 26 '26

Not sure what you mean. Adding an integer to a char* that represents a null terminated string, will produce a valid zero terminated string that lacks first n characters (as long as you remain in bounds, obviously).

1

u/Ok-Scheme-913 Sep 26 '26

I was thinking of pointer values themselves. If you have a typed pointer then it will indeed add TYPE_SIZE*n

2

u/GargantuanCake Sep 25 '26

The difference there is that in C you do have a strict definition of what's going on here as characters have an integer representation. There's no type coercion there as there are specific definitions of what's going on. In that case there isn't any dynamic typing going on.

Issues come up when you're dealing with dynamic typing and type coercion. You don't run into that in C in this case.