Thread: Using pointers

  1. #1
    Registered User
    Join Date
    Jun 2023
    Posts
    3

    Using pointers

    Is there a way to write strcat(p,q); using pointers?

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Seriously? strcat() does use pointers to both the source and destination! In a browser enter, "https://man7.org/linux/man-pages/man3/strcat.3p.html" and read the details presented there!

    strcat() is part of thr Standard Library so is the same on any Standards compliment compiler no matter what O/S.

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by Patrick149 View Post
    Is there a way to write strcat(p,q); using pointers?
    Yes, it is possible.

    If you are talking about defining your version of strcat(), how would you approach it? If you didn't use pointers how would you write it? How would you then implement it using pointers?

    Have you tried to write any code? We can't write it for you. We can only give you guidance and advise.

  4. #4
    Registered User
    Join Date
    May 2020
    Posts
    23
    Here's part of strcat's manual page from MacOS.



    NAME
    strcat, strncat – concatenate strings


    LIBRARY
    Standard C Library (libc, -lc)


    SYNOPSIS
    #include <string.h>


    char *
    strcat(char *restrict s1, const char *restrict s2);


    char *
    strncat(char *restrict s1, const char *restrict s2, size_t n);


    The asterisks signify pointers.

  5. #5
    Registered User
    Join Date
    Apr 2021
    Posts
    140
    You could certainly write strcat using pointers. But why would you want to? Far better to write strecat:

    Code:
    #define EMPTY    /*EMPTY*/
    
    char *
    strecat(
        char *dst,
        const char *src)
        {
        if ($is_null(dst)) 
            return dst;
        while (*dst)
            ++dst;
        if ($is_null(src))
            return dst;
        while ($not_zero(*dst++ = *src++))
            EMPTY;
        return dst;
        }

  6. #6
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by aghast View Post
    You could certainly write strcat using pointers. But why would you want to? Far better to write strecat:

    Code:
    #define EMPTY    /*EMPTY*/
    
    char *
    strecat(
        char *dst,
        const char *src)
        {
        if ($is_null(dst)) 
            return dst;
        while (*dst)
            ++dst;
        if ($is_null(src))
            return dst;
        while ($not_zero(*dst++ = *src++))
            EMPTY;
        return dst;
        }
    Please make sure that you post code that will compile on someone else's computer!

    "EMPTY;" should be replaced with a simple standard, ';' Everyone knows what that means!

    $is_null(dst) Not defined!
    $not_zero(*dst++ = *src++) Not defined!

    Source for the glibc version 2.39, strcat():
    Code:
    #include <string.h>
    
    #undef strcat
    
    #ifndef STRCAT
    # define STRCAT strcat
    #endif
    
    /* Append SRC on the end of DEST.  */
    char *
    STRCAT (char *dest, const char *src)
    {
      strcpy (dest + strlen (dest), src);
      return dest;
    }
    libc_hidden_builtin_def (strcat)
    I'll stick to the simpler Standard Library version!

    No need to reinvent the wheel, unless there is a legitimate reason to do so.
    Last edited by rstanley; 03-21-2024 at 12:55 PM.

  7. #7
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by BillMcEnaney View Post
    Here's part of strcat's manual page from MacOS.

    The asterisks signify pointers.[/FONT][/COLOR]
    strcat() is a C Standard Library function. As such, the prototype for this and all other C Standard Library functions, will be the same on Windows, Linux, UNIX, MacOS, and all other Standards Compliant compilers, on any O/S. No need to post it here.

    You can see the prototype for strcat() and all other Standard Library functions, online, by typing in"man strcat" (Or any other function), in any browser! (Use man7.org, and stay away from die.net, which has not been updated for many years!)
    Last edited by rstanley; 03-21-2024 at 12:51 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 08-29-2015, 01:15 PM
  2. Replies: 43
    Last Post: 05-23-2013, 03:01 PM
  3. Storing function pointers in generic pointers
    By Boxknife in forum C Programming
    Replies: 6
    Last Post: 08-01-2009, 01:33 PM
  4. Pointers to objects -- passing and returning pointers
    By 1veedo in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2008, 11:42 AM
  5. weak pointers and use_count smart pointers
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 07-29-2006, 07:54 AM

Tags for this Thread