tricky problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pras.vaidya@gmail.com

    #1

    tricky problem

    Hi , below given question was asked to me during an interview and i
    figured it out little tricky . It would be a great help if anyone
    could solve it.

    Code : -
    main()
    {
    char *s1="abcd",*s2= NULL;

    /* From here you call a function copy which has return type void .
    Simple task is to copy s1 into s2 . */

    copy(&s1,&s2);

    }

    copy(char **s,char **t)
    {

    ----- NORMAL COPY STATEMENTS --------


    /* But here we have to malloc " t " compulsory . Now what changes
    should i do so that chage will reflect in main .I have to print in main
    only. */

    }

  • Charles Mills

    #2
    Re: tricky problem



    pras.vaidya@gma il.com wrote:[color=blue]
    > Hi , below given question was asked to me during an interview and i
    > figured it out little tricky . It would be a great help if anyone
    > could solve it.
    >
    > Code : -
    > main()
    > {
    > char *s1="abcd",*s2= NULL;
    >
    > /* From here you call a function copy which has return type void .
    > Simple task is to copy s1 into s2 . */
    >
    > copy(&s1,&s2);
    >
    > }
    >[/color]

    Seems like trick question, how about a shallow copy?

    void
    copy(char **s,char **t)
    {
    *t = *s;
    }

    Don't forgot a function prototype for 'copy' and the correct
    declaration of 'main'.

    -Charlie

    Comment

    • junky_fellow@yahoo.co.in

      #3
      Re: tricky problem



      pras.vaidya@gma il.com wrote:[color=blue]
      > Hi , below given question was asked to me during an interview and i
      > figured it out little tricky . It would be a great help if anyone
      > could solve it.
      >
      > Code : -
      > main()
      > {
      > char *s1="abcd",*s2= NULL;
      >
      > /* From here you call a function copy which has return type void .
      > Simple task is to copy s1 into s2 . */
      >
      > copy(&s1,&s2);
      >
      > }
      >
      > copy(char **s,char **t)
      > {
      >
      > ----- NORMAL COPY STATEMENTS --------
      >
      >
      > /* But here we have to malloc " t " compulsory . Now what changes
      > should i do so that chage will reflect in main .I have to print in main
      > only. */
      >
      > }[/color]

      you may write the copy function as follows:

      #include <stdio.h>
      #include <string.h>
      #include <stdlib.h>
      int
      copy( char **s, char **t)
      {
      size_t len ;
      len = strlen(*s);

      *t = malloc(len + 1);
      strcpy(*t,*s);
      return(0);
      }

      Also, you don't need to pass the address of s1. Passing s1 is
      sufficient.
      If you pass s1 to copy, then you can use following function.

      int
      copy ( char *s, char **t)
      {
      size_t len ;
      len = strlen(s);
      *t = malloc(len + 1);
      strcpy(*t,s);
      return(0);
      }

      Also, string literals may be placed in read only memory and so won't
      be modified. In that case you don't even need to allocate the
      space for s2.

      int
      copy(char *s,char **t)
      {
      *t = s;
      return(0);
      }

      Comment

      • Hash

        #4
        Re: tricky problem

        main()
        {

        char *s1="abcd",*s2= NULL;

        copy(&s1,&s2);

        printf("%s\n",s 2);
        }

        void copy(char **s, char **t)
        {

        *t=(char *)malloc(4);
        strcpy(*t,*s);
        }




        <pras.vaidya@gm ail.com> wrote in message
        news:1121836474 .657309.268140@ o13g2000cwo.goo glegroups.com.. .[color=blue]
        > Hi , below given question was asked to me during an interview and i
        > figured it out little tricky . It would be a great help if anyone
        > could solve it.
        >
        > Code : -
        > main()
        > {
        > char *s1="abcd",*s2= NULL;
        >
        > /* From here you call a function copy which has return type void .
        > Simple task is to copy s1 into s2 . */
        >
        > copy(&s1,&s2);
        >
        > }
        >
        > copy(char **s,char **t)
        > {
        >
        > ----- NORMAL COPY STATEMENTS --------
        >
        >
        > /* But here we have to malloc " t " compulsory . Now what changes
        > should i do so that chage will reflect in main .I have to print in main
        > only. */
        >
        > }
        >[/color]


        Comment

        • Allan Bruce

          #5
          Re: tricky problem


          "Hash" <a@b.com> wrote in message
          news:A2nDe.8896 $uK7.6244@news. cpqcorp.net...[color=blue]
          > main()
          > {
          >
          > char *s1="abcd",*s2= NULL;
          >
          > copy(&s1,&s2);
          >
          > printf("%s\n",s 2);
          > }
          >
          > void copy(char **s, char **t)
          > {
          >
          > *t=(char *)malloc(4);
          > strcpy(*t,*s);[/color]

          and now you have just caused a memory access problem, "abcd" is 5 chars
          long - remember the \0 at the end ;-)
          Allan
          [color=blue]
          > }
          >[/color]


          Comment

          • Chris Dollin

            #6
            Re: tricky problem

            Hash top-posted (BAD Hash, no biscuit):
            [color=blue]
            > main()
            > {
            >
            > char *s1="abcd",*s2= NULL;
            >
            > copy(&s1,&s2);
            >
            > printf("%s\n",s 2);
            > }
            >
            > void copy(char **s, char **t)
            > {
            >
            > *t=(char *)malloc(4);
            > strcpy(*t,*s);
            > }[/color]

            BOOM.

            (a) dangerous cast of `malloc`.

            (b) no `#include <stdlib.h>`.

            (c) no check for malloc returning null.

            (d) Copying 5 characters (`a`, `b`, `c`, `d`, 0) into
            a mallocation with room only for 4.

            --
            Chris "electric hedgehog" Dollin
            It's called *extreme* programming, not *stupid* programming.

            Comment

            • Flash Gordon

              #7
              Re: tricky problem

              Hash wrote:

              Rude top posting fixed. Your reply belongs *under* the text you are
              replying to.
              [color=blue]
              > <pras.vaidya@gm ail.com> wrote in message
              > news:1121836474 .657309.268140@ o13g2000cwo.goo glegroups.com.. .
              >[color=green]
              >>Hi , below given question was asked to me during an interview and i
              >>figured it out little tricky . It would be a great help if anyone
              >>could solve it.[/color][/color]

              If you are ready for a C programming job then, IMHO, this is not tricky.
              [color=blue][color=green]
              >>Code : -
              >>main()
              >>{
              >>char *s1="abcd",*s2= NULL;
              >>
              >>/* From here you call a function copy which has return type void .
              >>Simple task is to copy s1 into s2 . */
              >>
              >> copy(&s1,&s2);
              >>
              >>}
              >>
              >>copy(char **s,char **t)
              >>{
              >>
              >> ----- NORMAL COPY STATEMENTS --------
              >>
              >>
              >> /* But here we have to malloc " t " compulsory . Now what changes
              >>should i do so that chage will reflect in main .I have to print in main
              >>only. */
              >>
              >>}
              >>[/color][/color]

              Include the appropriate headers for the library functions you are using,
              otherwise bad things can (and sometimes do) happen.

              #include <string.h>
              #include <stdlib.h>

              Since your copy function is defined after it is used, provide a
              prototype declaration to ensure everything ties together nicely.

              void copy(char **s, char **t);
              [color=blue]
              > main()[/color]

              main returns an int, tell the compiler this. Especially as in C99
              implicit int is no longer allowed.

              int main(void)
              [color=blue]
              > {
              >
              > char *s1="abcd",*s2= NULL;
              >
              > copy(&s1,&s2);
              >
              > printf("%s\n",s 2);
              > }
              >
              > void copy(char **s, char **t)
              > {
              >
              > *t=(char *)malloc(4);[/color]

              The return value of malloc is a void* so the cast is not required. If
              the compiler complains without the cast then either you have not
              included stdlib.h or you are compiling as C++.
              [color=blue]
              > strcpy(*t,*s);[/color]

              Bang. You need to allow space for the '\0' termination of the string as
              well. Also you are not checking for malloc failing.

              size_t len = strlen(*s) + 1; /* +1 for the null termination */
              *t = malloc( len );
              if (*t != NULL)
              memcpy(*t, *s, len); /* might as well use memcpy as we know

              the length already. */

              /* Note that on malloc failure the destination is set NULL so
              the caller needs to check this before using the value. */
              [color=blue]
              > }[/color]
              --
              Flash Gordon
              Living in interesting times.
              Although my email address says spam, it is real and I read it.

              Comment

              • martinaw@gmail.com

                #8
                Re: tricky problem

                #include <stdio.h>
                #include <stdlib.h>

                copy(char **, char **);

                main() {
                char *s1="abcd", *s2=NULL;

                copy(&s1, &s2);

                /*
                * test it ;)
                */
                printf("%s\n", s2);
                }

                copy(char **s, char **t) {
                *t=malloc(strle n(*s)+1);
                if(*t!=NULL)
                strcpy(*t, *s);
                }

                Comment

                • Grumble

                  #9
                  Re: tricky problem

                  martinaw@gmail. com wrote:[color=blue]
                  > #include <stdio.h>
                  > #include <stdlib.h>
                  >
                  > copy(char **, char **);
                  >
                  > main() {
                  > char *s1="abcd", *s2=NULL;
                  >
                  > copy(&s1, &s2);
                  >
                  > /*
                  > * test it ;)
                  > */
                  > printf("%s\n", s2);
                  > }
                  >
                  > copy(char **s, char **t) {
                  > *t=malloc(strle n(*s)+1);
                  > if(*t!=NULL)
                  > strcpy(*t, *s);
                  > }[/color]

                  Did you try to compile this program with e.g. gcc -Wall -Wextra ??

                  Comment

                  Working...