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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • novice

    #1

    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

  • santosh

    #2
    Re: Help analyse [ char *--*++ptr + 3 ]

    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.

    Comment

    • Frederick Gotham

      #3
      Re: Help analyse [ char *--*++ptr + 3 ]

      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

      Comment

      • Ancient_Hacker

        #4
        Re: Help analyse [ char *--*++ptr + 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.

        Comment

        • Keith Thompson

          #5
          Re: Help analyse [ char *--*++ptr + 3 ]

          "santosh" <santosh.k83@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) kst-u@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.

          Comment

          • Old Wolf

            #6
            Re: Help analyse [ char *--*++ptr + 3 ]

            Keith Thompson wrote:
            "santosh" <santosh.k83@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.

            Comment

            • jmcgill

              #7
              Re: Help analyse [ char *--*++ptr + 3 ]

              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


              Comment

              • Keith Thompson

                #8
                Re: Help analyse [ char *--*++ptr + 3 ]

                "Old Wolf" <oldwolf@inspir e.net.nzwrites:
                Keith Thompson wrote:
                >"santosh" <santosh.k83@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) kst-u@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.

                Comment

                • Christopher Benson-Manica

                  #9
                  Re: Help analyse [ char *--*++ptr + 3 ]

                  novice <novice.sam@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.

                  Comment

                  Working...