Is there a way to write strcat(p,q); using pointers?
Is there a way to write strcat(p,q); using pointers?
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.
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.
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.
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():I'll stick to the simpler Standard Library version!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)
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.
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.