>if (!pointer)
>if (pointer == NULL)
>if (NULL == pointer)
Which is better?
If (!pointer)
The first one.
Nullptr masterrace
>using a legacy language that allows the concept of null pointers
>if (NULL == pointer)
#1 is idiomatic
#2 is not
#3 is paranoid tier
When in doubt, use 1 or 3.
>using pointers in 2018
use a reference ;^)
if thing is not:
First
Go back to web dev
switch (pointer) {
case NULL:
default:
}
I do 2 because splint complains if you do 1.
i will tomorrow morning :|
>if (pointer == NULL)
I can handle #1 though.
pointer & NULL ?
always results in null
It's not a valid operation to begin with.
>if (pointer == NULL)
is the cleanest to understand
>if (!pointer)
hides type (pointer vs integer) but for some reason is always recommended
if (NULL == pointer)
is the recommended for code that needs to be safe and not crash because you could make a mistake (it's happened) where you accidentally
if (pointer = NULL)
and some bullshit happens like UID is set to 0 or your computer explodes
I prefer this style because it means you can't mistype NULL = pointer
When comparing with a literal, literal should ALWAYS go on the left hand side.
Compilers have been warning about that shit for ages. It's a non-issue.
All it does now is make your logic harder to follow, because it doesn't read as naturally.
Yoda conditions best conditions
>because it doesn't read as naturally.
to (You)
Yoda comparison
This.
Don't do this: while( ( c = getchar() ) != NULL ) {
Thank you.
>in Java everything is a pointer
>Don't do this:
Doing it that way is actually the best way to do it.
Everything is a pointer in nearly every language which isn't C or C++.
Assignments within conditionals is bad practise.
In the case of loops, it actually has a benefit: it reduces code duplication.
The way you'd have to write that without putting it inside the conditional is
int c = getchar();
while (c != EOF) {
// Do thing
c = getchar();
}
int c;
for(;;)
{
/* do something */
c = getchar();
/* do some other thing */
if(isEof(c))
{
break;
}
/* do more things */
}
Get on my level.
not using C
Fuck off. It's fine as long as the code is readable.
>what is do while
while(*dest++ = *src != 0);
What the fuck is this language
*src++
god damnit
Literally not equivalent to
>He doesn't understand expressions
Whats an expression, I just want to write some code
dumb sepples poster
cuck
hi!
if the pointer is a "null pointer", the value of pointer is literally 0
so you can check if(pointer == 0)
since 0, when cast to bool is false, if(!pointer) is indeed the best choice, as pointed out.
to quote SE:
>NULL is not a built-in constant in the C or C++ languages. In fact, in C++ it's more or less obsolete, just use a plain literal 0 instead, the compiler will do the right thing depending on the context.
what are built-ins
>if the pointer is a "null pointer", the value of pointer is literally 0
nice bait
pointers are just numbers, senpai
>in C++ it's more or less obsolete, just use a plain literal 0 instead
Sounds derprecated for C++11+. Source?
nvm, found it and it explicitly says it's outdated
none of the code in K & R is readable
That have a type.
NULL is a macro, it can be void* or int
if (!pointer) unless you really want to be explicit for some reason (for example you don't know pointer is actually a pointer, comparing to NULL reminds you it is)
TYPE MY ANUS
with pleasure ;)
>pointers are just numbers
listen up, just because ptr==0 evaluates to true (1) for ptr being a nullpointer, doesnt mean that its literal value is 0.
it just means that 0 in pointer context evaluates to the same as any nullpointer.
c-faq.com
softwareengineering.stackexchange.com
C pleb here
what's the difference between 2 and 3?
>while(*dest++ = *src++ != 0);
Why in God's name would you do this.
Operations do not commute in this language.
NULL is an rvalue (you cannot assign something to it) and pointer is a potential lvalue.
Because elitism.
to prevent the common typo
if (pointer=NULL)
(assignment, not comparison)
but it's kind of a nonissue nowadays, see
more important, is that two sequence points at once?
The second one because it's more natural to read
The hell?
getchar doesn't return a pointer
>based java
Curse you Java for not allowing pointers to primitive types and forcing me to create object wrappers when I want to return more than one primitive types from a function
>C++
References were a mistake
>if (NULL == pointer)
this implies that nothing is equal to possibly something, which is not logical.
>if (pointer == NULL)
if possibly something is equal to nothing, is much more logical.
>Assignments within conditionals is bad practise.
Wrong!
There is literally a paragraph in K&R where they explain why this is the best form
>doesnt mean that its literal value is 0.
it literally does tho; is this bait?
Actually in some architectures, NULL is not 0.
There is a question on theC FAQ about this
this
Both the first and the last one prevents accidental assignments, but I prefer the last one because the intention is more explicit.
it literally is
>listen up, just because i==3 evaluates to true (1) for i being a three, doesnt mean that its literal value is 3.
>it just means that 3 in int context evaluates to the same as any three.
lad...
first
why on earth would anyone use the following twos?
>doesnt mean that its literal value is 0
Don't think in C terms dipshit. Think of what is actually happening on the CPU.
If you're programming for one architecture, sure.
Acadamic circlejerking is the cancer ruining software development.
>he says on an anime website
there's no NULL on the CPU, shitstain
I said 0, not null.
First because NULL is not a C keyword.
Your right, NULL is not a keyword. It is a macro defined in stddef.h.
Also, NULL is not guaranteed to be zero. Second one. Bitch.
>Also, NULL is not guaranteed to be zero.
Yes it is. It is not guaranteed to be an all-zero bit pattern, but it IS guaranteed to be the C value zero, however that is represented in bits.