There is no such thing as perfect co-
There is no such thing as perfect co-
Other urls found in this thread:
pic not related
...
Everything that needs to be said about your code bait has been said in the previous thread.
warosu.org
>Old style function declarations
She even has tests!
There is no such thing as perfect co-
>when you think you're a genius for learning pointer arithmetic
Not a coder, is this how the code works?
strlen accepts a string and returns its length, presumably. There's a const declaration that I have no idea what it does, then a register declaration which again I have no idea what it does. Then a for loop that does increments s which starts as a pointer to the start of the string, and as long as its content is not the end-of-string char, increments the address by 1.
Then it returns the difference between the incremented address and the string address, which is the string length.
The readability of this code is very low. One-liners are fun, but code needs to be read and edited conveniently. Besides, writing out that for loop in a more readable way doesn't make it any slower when compiled; the compiler doesn't care if code is written out in one line or several if it does exactly the same thing, from an assembly code point of view.
>2017
>not using C++17 constexprs
>There's a const declaration that I have no idea what it does,
Guarantees that the pointee isn't modified
>then a register declaration which again I have no idea what it does
It's a compiler hint. It basically says "I'm going to use this variable A LOT, you should probably use a register for it rather than a memory location"
> Then a for loop that does increments s which starts as a pointer to the start of the string, and as long as its content is not the end-of-string char, increments the address by 1.
Correct
>Then it returns the difference between the incremented address and the string address, which is the string length.
Correct
>The readability of this code is very low
It is very good, you're just not experienced with C
Yes, you've basically got it correct.
>const declaration
It's saying that it won't be modified.
>register declaration
It's an optimisation feature. Just ignore it, it doesn't really make any difference for this code.
>The readability of this code is very low
An experienced C programmer would be very well versed with pointers, and would not see that code as "tricky".
I don't know much about C. Here's what I don't understand:
If s is declared as const, it means it can't be changed, right? How can you then do s = str or ++s? What's the purpose of it being const then?
The pointed to value is const, not the pointer itself.
const char *ptr = whatever;
ptr += 1; // This is fine
*ptr += 1; // This isn't
The data/pointee is unmodifiable, not the pointer itself.
char const *s; // can modify pointer, cannot modify pointee
const char *s; // alternative form of the above
char * const s; // cannot modify pointer, can modify pointee
char const * const s; // cannot modify neither pointer nor pointee
s++ good
*s++ bad
> declaring the parameter outside of the parameter list
Ok I see now thank you. So it's declared as const to make sure the chars aren't modified while the pointer is moving across the string. Is that correct? If so, why doesn't the const declaration of str already provide that?
>So it's declared as const to make sure the chars aren't modified while the pointer is moving across the string. Is that correct
Yes
>If so, why doesn't the const declaration of str already provide that?
Failing to declare s as a similar pointer type as str and then attempting to assign str to s is undefined behaviour and will most likely result in a compiler warning or error.
>So it's declared as const to make sure the chars aren't modified while the pointer is moving across the string. Is that correct?
Yes.
>why doesn't the const declaration of str already provide that?
It does. It's just that you can't 'discard' a type qualifier (e.g. const) by assigning it to another variable, so you need to specify it on each variable.
const char *str = whatever;
char *s = str; // Compiler will complain
Ok that makes sense. Thanks for the explanation.
...
>C++
>being able to force a compiler to inline
>the compiler having an option to refuse
>being able to force inlining when the compiler refuses
How deep does the hole go?
lol'd
You have to go back pajeet
>coder
You left out a crucial part of the original meme.
kys degenerate
>but code needs to be read and edited conveniently
You're expected to know your shit when you do C development, if pointer arithmetic is too hard for you, you HAVE to spend time learning it or fuck off to webdev.
We already have too much buffer overflow problems made by careless developers to deal with more incompetent developers.
I recognize that co-!