[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

What's the diference in the real world between i++ and ++i

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 16
Thread images: 1

File: 1436739314641.png (362KB, 838x574px) Image search: [Google]
1436739314641.png
362KB, 838x574px
What's the diference in the real world between i++ and ++i

in practical terms, I already know both definitions.
>>
If you assign it to something, it's between doing the increment before or after the assignment.
>>
>>61186190
>i = 0;
>thisValue = i++;

>i = 0;
>thisValue = ++i;
>>
>>61186214
I know the definition of both.

what does this mean in real working code?
>>
>>61186223
both give the same result, right?
>>
>>61186225
What the fuck are you trying to ask? Elaborate.
>>
>>61186239
give me some real working example.
>>
>>61186190
>>61186214
>>61186223
>>61186225
>>61186231
>>61186239
fuck off, underage kids.
>>>/reddit/
>>
>>61186254
It is mostly personal preference or coding style of your comapny, you mongoloid nigger. Compilers should optimize that shit anyways. It CAN improve performance in some rare cases, but they are neglectable
>>
>>61186258
>>61186272
so basically is the same fucking shit.
>>
Usually only relevant in loops.

x = 0;
array[x++];

x = 0;
array[++x];

The first is array[0] and the second is array[1], so you could have the same starting variable (x) but have some condition that, for example, ignores the first value of an array. Why would you do it like that? I don't know but you could.
>>
Consider this example check to find the end of a null-terminated buffer in a while loop:
if (*current_position++ == '\0') {
return current_position;
}

If this were used, current_position would be incremented *after* being dereferenced and checked. When we reach the end of the buffer, we set current_position to point to the byte *after* the end of the buffer, which is dangerous if that address is used, since it could result in a segfault. The correct way to do this:
if (*++current_position == '\0') {
return current_position;
}

This way, current_position is incremented *first*, so when the end of the buffer is found, we return the correct address.
>>61186287
Don't listen to those retards.
>>
>>61186306
>>61186300
that makes sense I guess.
thanks.
>>
>>61186339
Another example.

value = 5;
previous = value++;
// previous is equal to 5

value = 5;
previous = ++value;
// previous is equal to 6
>>
>>61186382
why does the assigment is given before the increment?
>>
>>61186190
Depends on compilers.

int i = 10;
writeln(++i); should print 11 --it performs the unary function (++) before i is used
writeln(i++) sould print 10. But if you call i again, it'll be 11. The unary function is performed afterwards.
Thread posts: 16
Thread images: 1


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.