19
Lurisan
7y

Output please 😅😅

Comments
  • 2
    Yea, 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);"
  • 3
    Won't compile due to i[s]
  • 3
    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]
  • 3
    @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.
  • 1
  • 0
    I suggest everyone to run it on a compiler 🙄🙄
  • 4
    @512GB that actually is legal syntax and does the same as s[i]
  • 0
    @512GB Thought the same. But then I tested it and it does compile and even work.
  • 0
    What did you get?? @mw108
  • 2
  • 2
    All refer to the same character

    s[i]
    *(s+i)
    *(i+s)
    i[s]
  • 0
    @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)
  • 1
    @Lurisan I know about the first three, but how is the last one valid?
  • 0
    C compiler supports that too, I guess @endor
  • 1
    @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
  • 0
    The 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"
  • 0
    Following 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
  • 2
    @512GB That's perfectly valid C code

    s[i] = *(s+i) = *(i+s) = i[s]
  • 2
    @AlgoRythm You are incorrect. 's' is pointer type, not string and 'i' is offset.

    s[i] is just syntactic sugar for *(s+i)
  • 0
    @endor i[s] is also correct and compiles without issues.

    s[i] = *(s+i) = *(i+s) = i[s]
  • 0
    @mw108 We even have a obfuscated C contest

    http://www.ioccc.org
  • 1
    @lazy-dev Obviously more of a c expert than me! What a funny language.
  • 1
    mmmm
    aaaa
    nnnn
  • 1
    @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*
  • 0
    too late. but from experience i knew each one has same output.
  • 0
    It 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.
  • 0
    indent please
  • 0
    Doesn't compile: error: cannot compile file type ".jpg"
Add Comment