473,395 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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

Oct 8 '06 #1
8 2068
novice wrote:
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.

Oct 8 '06 #2
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

--

Frederick Gotham
Oct 8 '06 #3
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.

Oct 8 '06 #4
"santosh" <sa*********@gmail.comwrites:
novice wrote:
>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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 8 '06 #5
Keith Thompson wrote:
"santosh" <sa*********@gmail.comwrites:
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.
Besides, you shouldn't consider jobs at places where interviewers
ask such stupid questions.
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.

Oct 9 '06 #6
Old Wolf wrote:
>>> puts( *--*++ptr + 3 ); <--------------------------- HELP ...!
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
Oct 9 '06 #7
"Old Wolf" <ol*****@inspire.net.nzwrites:
Keith Thompson wrote:
>"santosh" <sa*********@gmail.comwrites:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 9 '06 #8
novice <no********@gmail.comwrote:
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".)

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Oct 9 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: sathya | last post by:
I have come across the following code which treys to implement the strcat function. It may be wrong or correct, but it is using integer zero instead of appending the C string terminator '\0'. ...
19
by: Rafal Dabrowa | last post by:
What does mean such declaration: void f( char (*ptr) ); Is this the same as void f( char **ptr ); or not ?
10
by: Amit | last post by:
char *str1="amit"; char str2="mehta" strcat(str1,str2); It will crash, I feel str1 will be stored in code section. Once memory is allocated, you cannot change or append into this. Please...
10
by: fei.liu | last post by:
Consider the following sample code char * ptr = "hello"; char carray = "hello"; int main(void){ } What does the standard have to say about the storage requirement about ptr and carray? Is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.