Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
mw1082667yYea, C is weird and funny at the same time. Reminds me on the "C was a hoax" story:
"We stopped when we got a clean compile on the following syntax:
for(;P("\n"),R-;P("|"))for(e=3DC;e-;P("_"+(*u++/8)%2))P("|"+(*u/4)%2);" -
H-how can you use s with i? "s+i"??? What????
s is a string??? And i is an integer??? And you can't just concatenate like that in c?????
I'm a bit confused. Not to fucking mention i[s] -
endor57517y@AlgoRythm s is a pointer to a string - a memory address. *(s+i) would basically give you the content of the i-th element inside the array s (because you can add an integer to a memory address to obtain another memory address).
i[s] is wrong though. -
endor57517y@Krokoklemme wait, what? Why is that valid? Is it some compiler optimization that corrects it at compile time? Because the way I would normally read it, i[s] means trying to access some random memory address after i's address, which should result in a segfault (unless that random address happens to be accessible by the program by pure coincidence)
-
@endor that's because the brackets are just syntactical sugar for pointer arithmetic (an addition, in this case)
It doesn't matter if you do s+i or i+s, the result will be the same -
skprog19167yThe c Compiler on my phone can't handle this kind of code it just outputs "user suggesting code is error" and the one on my desktop says "lurisan is not a valid artifact"
-
skprog19167yFollowing for c4droid
#include <stdio.h>
using namespace std;
int main(){
char s[] = "the man";
int i ;
for(i=0;i<7;++i) {
printf("\n%c",s[i]);
}
return 0; }
T
H
E
M
A
N -
lazy-dev137y@AlgoRythm You are incorrect. 's' is pointer type, not string and 'i' is offset.
s[i] is just syntactic sugar for *(s+i) -
lazy-dev137y
-
endor57517y@Krokoklemme yeah, thing is I interpreted it as:
s[i] == content of memory address s + (value of i)
i[s] == content of memory address i (aka &i) + (value of s)
So in the first case you'd be adding an int to a memory address, while in the second one you'd be adding two memory addresses together. But I guess that's not how it works, since we have working proof *shrug* -
xsacha4317yIt is perfectly legal and makes sense and there's valid reasons for all that syntax. That's just C. Love it.
For the people who don't understand C, it is just the memory references and so accessing an index of a value simply adds the numbers together.
You are looking at several variants of addition if memory references and conversion from val to ref
There's no stupid checks to tell you things like i[s] are out of bounds. It just adds the values and trusts you.
If you are trustworthy, it all works.
Related Rants
Output please 😅😅
undefined
output
programming
c