# Guess the code!

Show 80 post(s) from this thread on one page
Page 6 of 7 First 1234567 Last
• 11-11-2006
Mario F.
An easy one:

Code:

```int a[] = {11,12,13,14,15}; int* ap = a - 1;```
• 11-11-2006
Dave_Sinkula
That's just evil.
• 11-11-2006
Mario F.
And yet that's correct :)
• 11-11-2006
CornedBee
Are you absolutely sure? The standard is far more precise about what operations are allowed on pointers and when than what any implementation actually enforces.

Hmm, according to the C faq link it's not correct.
• 11-11-2006
Mario F.
I meant Dave was correct on how he interpreted the code. It's a way to implement a unit-based array.

EDIT: I was actually unware at the fact that despite there existing no dereferencing it could still produce undefined behavior. But Dave hit it on the nail at the code usage.
• 11-11-2006
TriKri
Quote:

Originally Posted by Mario F.
An easy one:

Code:

```int a[] = {11,12,13,14,15}; int* ap = a - 1;```

But is there actually systems that behaves differenty than expected, trying to "wrap around" for example?
• 11-11-2006
Mario F.
> But is there actually systems that behaves differenty than expected, trying to "wrap around" for example?

Some people don't like to admit they were wrong, apparently.

While the link provided by Dave explains why it is undefined behavior, the following link from the Numerical Recipes book website shows an extract of the new Numerical Recipes for C++ book. Take a look at page 10. It's worth your time... although probably make you mad.

http://www.numerical-recipes.com/cpppages/chap1sel.pdf
• 11-11-2006
Dave_Sinkula
For fun:
Code:

```void foo(unsigned int value) {   unsigned int bit;   for ( bit = /* msb */(~0U >> 1) + 1; bit > 0; bit >>= 1 )   {       putchar("01"[!!(value & bit)]);   }   putchar('\n'); }```
• 11-11-2006
CornedBee
Ain't that just a base 2 itoa, printing directly?
• 11-11-2006
TriKri
Mario F: I think you missunderstod me, I was just being curious.
• 11-11-2006
Mario F.
Quote:

Originally Posted by TriKri
Mario F: I think you missunderstod me, I was just being curious.

Oh. I have. Sorry. I read that as a rethorical question.
• 11-11-2006
Dave_Sinkula
Quote:

Originally Posted by CornedBee
Ain't that just a base 2 itoa, printing directly?

Yeah. I was kind of shooting for something along the line of, "the !!(value & bit) results in a one or a zero if the selected bit is set in value or not, so you can safely index into the string literal", or something.
Code:

`putchar("01"[!!(value & bit)]);`
• 11-13-2006
Laserve
Quote:

Originally Posted by Dave_Sinkula
Yeah. I was kind of shooting for something along the line of, "the !!(value & bit) results in a one or a zero if the selected bit is set in value or not, so you can safely index into the string literal" [/code]

what if you'd do (value & bit) != 0 ? isnt that either 0 or 1 ?
• 11-13-2006
jafet
But!! isn't!! double!! exclaimation!! so!! leet!! ??!!
• 11-13-2006
manutd
Ah! Not the leet thing again!
Show 80 post(s) from this thread on one page
Page 6 of 7 First 1234567 Last