Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. problems returning/printing poiner

problems returning/printing poiner

Scheduled Pinned Locked Moved C / C++ / MFC
data-structurestutorialquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    doughyi8u
    wrote on last edited by
    #1

    I understand that the reason the ret variable will not print (it's at the end of the string when destination is passed) but can't figure out how to return destination out of the my_strcpy() function. I can make this work using array notation but not pointer arithmetic. Here's the code in question:

    #include #include #include char *my_strcpy(char *, const char *);

    int
    main()
    {
    char *strA = "This is a string";
    char *strB;
    char *ret;

    strB = malloc(strlen(strA)+1);
    ret = my\_strcpy(strB, strA);
    puts(ret);
    puts(strB);
    free(strB);
    return 0;
    

    }

    char *my_strcpy(char *destination, const char *source)
    {
    while (*source != '\0')
    {
    *(destination++) = *(source++);
    }
    *destination = '\0';
    return destination;
    }

    J L 2 Replies Last reply
    0
    • D doughyi8u

      I understand that the reason the ret variable will not print (it's at the end of the string when destination is passed) but can't figure out how to return destination out of the my_strcpy() function. I can make this work using array notation but not pointer arithmetic. Here's the code in question:

      #include #include #include char *my_strcpy(char *, const char *);

      int
      main()
      {
      char *strA = "This is a string";
      char *strB;
      char *ret;

      strB = malloc(strlen(strA)+1);
      ret = my\_strcpy(strB, strA);
      puts(ret);
      puts(strB);
      free(strB);
      return 0;
      

      }

      char *my_strcpy(char *destination, const char *source)
      {
      while (*source != '\0')
      {
      *(destination++) = *(source++);
      }
      *destination = '\0';
      return destination;
      }

      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      Just use a local variable for copying or save the value of destination in a local variable to be returned upon exit:

      char *my_strcpy(char *destination, const char *source)
      {
      char *ret = destination;
      while (*source != '\0')
      {
      *destination++ = *source++;
      }
      *destination = '\0';
      return ret;
      }

      1 Reply Last reply
      0
      • D doughyi8u

        I understand that the reason the ret variable will not print (it's at the end of the string when destination is passed) but can't figure out how to return destination out of the my_strcpy() function. I can make this work using array notation but not pointer arithmetic. Here's the code in question:

        #include #include #include char *my_strcpy(char *, const char *);

        int
        main()
        {
        char *strA = "This is a string";
        char *strB;
        char *ret;

        strB = malloc(strlen(strA)+1);
        ret = my\_strcpy(strB, strA);
        puts(ret);
        puts(strB);
        free(strB);
        return 0;
        

        }

        char *my_strcpy(char *destination, const char *source)
        {
        while (*source != '\0')
        {
        *(destination++) = *(source++);
        }
        *destination = '\0';
        return destination;
        }

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        In addition to Jochen's comments, you also need to allocate some memory to strB before calling your copy function. The code above will be copying into some random place and probably crash.

        One of these days I'm going to think of a really clever signature.

        J 1 Reply Last reply
        0
        • L Lost User

          In addition to Jochen's comments, you also need to allocate some memory to strB before calling your copy function. The code above will be copying into some random place and probably crash.

          One of these days I'm going to think of a really clever signature.

          J Offline
          J Offline
          Jochen Arndt
          wrote on last edited by
          #4

          He allocates memory using malloc(). You may have overlooked the line just above the copy call.

          L 1 Reply Last reply
          0
          • J Jochen Arndt

            He allocates memory using malloc(). You may have overlooked the line just above the copy call.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Oops! :doh:

            One of these days I'm going to think of a really clever signature.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups