Help analyse [ char *--*++ptr + 3 ]

N

novice

Hi geeks,
Can any body explain me how to analyse [ *--*++ptr + 3 ] int the
pollowing code

This is the question I was asked in the interview...

char *s[4] ={ "hello", "basic", "world", "program"};

char **sPtr[4] = { s+3, s+2, s+1, s };

char **tempPtr = sPtr;
//---------------------------------
puts( *--*++ptr + 3 ); <--------------------------- HELP ...!
//--------------------------------

Thanks in advance
 
S

santosh

novice said:
Hi geeks,
Can any body explain me how to analyse [ *--*++ptr + 3 ] int the
pollowing code

This is the question I was asked in the interview...

char *s[4] ={ "hello", "basic", "world", "program"};

char **sPtr[4] = { s+3, s+2, s+1, s };

char **tempPtr = sPtr;
//---------------------------------
puts( *--*++ptr + 3 ); <--------------------------- HELP ...!
//--------------------------------

This won't even compile. Besides, you shouldn't consider jobs at places
where interviewers ask such stupid questions. Nowadays, programming is
more about trying to derive simplicity out of complexity, not the
reverse.
 
F

Frederick Gotham

novice posted:
Hi geeks,

Is that how you justify your own lack of proficiency in your own mind; you
feel that you don't have to compare your own proficiency to ours if you use
a label which alienates us from you?

Ah just a though that came to mind. I was watching a documentary last night
about murderers, and the psychologists were saying that society likes to
label murderers as "insane" so that we don't have to fully comprehend or
accept what they do, and we're spared the realisation of the reality of
what a normal person actually does and is capable of.

*--*++ptr + 3

+
/ \
3 *
\
-- (pre)
\
*
\
++ (pre)
\
ptr
 
A

Ancient_Hacker

Unless your family is starving, I would just say, "no thanks, I don't
work that way" and walk out of that interview. It suggests strongly
the place is out of control.
 
K

Keith Thompson

santosh said:
novice said:
Hi geeks,
Can any body explain me how to analyse [ *--*++ptr + 3 ] int the
pollowing code

This is the question I was asked in the interview...

char *s[4] ={ "hello", "basic", "world", "program"};

char **sPtr[4] = { s+3, s+2, s+1, s };

char **tempPtr = sPtr;
//---------------------------------
puts( *--*++ptr + 3 ); <--------------------------- HELP ...!
//--------------------------------

This won't even compile. Besides, you shouldn't consider jobs at places
where interviewers ask such stupid questions. Nowadays, programming is
more about trying to derive simplicity out of complexity, not the
reverse.

It compiles (with a couple of warnings) if I change "ptr" to "sPtr" in
the last line. It then crashes with a segmentation fault.

I'm sure the code the OP showed us isn't the exact code he was asked
about in the interview unless he was given a soft copy of the
interview questions and was able to copy-and-paste this one into his
newsreader.

But consider a context like this:

We just fired the guy who held the job we're considering you for,
because he wrote such badly convoluted code (he thought he was
being "clever"). His code works, as far as we can tell by testing
it, but it doesn't do us much good because nobody else can
maintain it. We need someone to figure out what it's actually
doing and re-write it in legible and maintainable C.

If the OP is expected to *write* code like that, he should run away
very fast. But a good C programmer should be able to *read* bad code
and *write* good code. It is, as you say, about trying to derive
simplicity out of complexity.
 
O

Old Wolf

Keith said:
santosh said:
novice said:
char *s[4] ={ "hello", "basic", "world", "program"};
char **sPtr[4] = { s+3, s+2, s+1, s };
char **tempPtr = sPtr;
//---------------------------------
puts( *--*++ptr + 3 ); <--------------------------- HELP ...!
//--------------------------------

This won't even compile.

It compiles (with a couple of warnings) if I change "ptr" to "sPtr" in
the last line. It then crashes with a segmentation fault.

The code contains a constraint violation requiring a diagnostic.

I think this is what people mean when they say "won't even compile",
given that compilers are allowed to compile even random collections
of characters.

I think this is a reasonable question, if we fix the presumed errors
in the OP's recollection. It does a good job of testing whether
someone understands pointer manipulation when there are
several levels of indirection.

Indeed, the fact that he even went and posted here asking what
the code does, shows that his level of C competence is below
what many companies would be looking for.
 
J

jmcgill

I wish you had a more accurate example. From what you posted it's
impossible to tell whether it's an obfuscated "Hello world" or something
else. But your statement in question, increments ptr first, then
dereferences it, decrements that value, dereferences that, adds 3 to
that value, which is hopefully a char *, and sends it to puts.
Forgetting about char *'s for the moment, it's something like this:

void **p0, *p1;
ptr += 1;
p0 = *ptr;
p0 -= 1;
p1 = *p0;
p1 += 3
 
K

Keith Thompson

Old Wolf said:
Keith said:
santosh said:
novice wrote:

char *s[4] ={ "hello", "basic", "world", "program"};
char **sPtr[4] = { s+3, s+2, s+1, s };
char **tempPtr = sPtr;
//---------------------------------
puts( *--*++ptr + 3 ); <--------------------------- HELP ...!
//--------------------------------

This won't even compile.

It compiles (with a couple of warnings) if I change "ptr" to "sPtr" in
the last line. It then crashes with a segmentation fault.

The code contains a constraint violation requiring a diagnostic.

I think this is what people mean when they say "won't even compile",
given that compilers are allowed to compile even random collections
of characters.
[...]

You're right, I missed the fact that at least one of the warnings was
actually about a constraint violation (that might cause some other
compiler to reject the code outright). (I used gcc, which often gives
warnings for constraint violations.)

I can imagine, though, that the original code (as opposed to what the
OP showed us) might have been legal (but unbearably ugly). If so,
figuring out what the code actually does could be a reasonable test of
someone's knowledge of C. 5 points for correctly interpreting the
code, 10 points for correctly saying that no sane person would write
it.
 
C

Christopher Benson-Manica

novice said:
This is the question I was asked in the interview...
char *s[4] ={ "hello", "basic", "world", "program"};
char **sPtr[4] = { s+3, s+2, s+1, s };
char **tempPtr = sPtr;
puts( *--*++ptr + 3 ); <--------------------------- HELP ...!

Perhaps you really were presented code something like

#include <stdio.h>

int main( void )
{
char *s[4] ={ "hello", "basic", "world", "program"};
char **sPtr[4];
char ***tempPtr = sPtr;

sPtr[0]=s+3;
sPtr[1]=s+2;
sPtr[2]=s+1;
sPtr[3]=s;
puts( *--*++tempPtr + 3 );
return 0;
}

? Starting with a correct program is always a good way to get a
correct answer. (No, it doesn't print "hello world".)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top