473,748 Members | 2,207 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2093
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*********@gm ail.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_Keit h) 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*********@gm ail.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*****@inspir e.net.nzwrites:
Keith Thompson wrote:
>"santosh" <sa*********@gm ail.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_Keit h) 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********@gma il.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)gma il.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
1563
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'. #include <stdarg.h> char *xstrcat(char *des, char *src, ...) { char *destination = des;
19
3485
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
1935
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 correct me If I am wrong..
10
5355
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 it a fair statement that char *ptr will take 4 more bytes (on 32bit platform) in DATA segment? I have found
0
9552
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9376
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9249
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8245
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4607
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4877
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3315
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.