Hello guys so I am learning C and I am creating the strcat
function and when I print out the values of dest
at the index i
concatenate a char at I get that char but when I return dest
and print it out back in my main
function the changes aren't reflected. Can someone please help me out? thanks.
#include <stdio.h>
#include <stdlib.h>
int size_s(char *str) {
int size = 0;
int index = 0;
while (str[index] != '\0') {
size = 1;
index = 1;
}
return (size 1);
}
/*
* @function: strcat
* @desc: Takes in two char pointers and concatenates them. provided the destination has enough size otherwise undefined behavior can occur. Overwrites the null terminator
*/
char *strcat_s(char *dest, char *source)
{
int index_of_src = 0;
int index_of_dest = size_s(dest);
while (source[index_of_src] != '\0') {
*(dest index_of_dest) = source[index_of_src];
index_of_src = 1;
index_of_dest = 1;
}
// Add Null terminator
*(dest (index_of_dest 1)) = '\0';
return dest;
}
int main(int argc, char **argv) {
char firstname[100];
scanf("%s", firstname);
char lastname[100];
scanf("%s", lastname);
int sizeofFirst = size_s(firstname);
printf("Sizeof first: %d\n", sizeofFirst);
int sizeofSecond = size_s(lastname);
printf("Sizeof second: %d\n", sizeofSecond);
char *concatinated = strcat_s(firstname, lastname);
printf("%s\n", concatinated);
}
CodePudding user response:
The function size_s
returns the index of the character after the zero-terminating character '\0' due to this return statement
return (size 1);
So in this while loop
int index_of_src = 0;
int index_of_dest = size_s(dest);
while(source[index_of_src] != '\0')
{
*(dest index_of_dest) = source[index_of_src];
index_of_src = 1;
index_of_dest = 1;
}
the array pointed to by the pointer dest
is filled after the terminating zero character '\0'
.
As a result this call of printf
printf("%s\n", concatinated);
outputs the initially stored string in the array firstname
.
Rewrite the function size_s
the following way
size_t size_s( const char *s )
{
size_t n = 0;
while ( s[n] != '\0' ) n;
return n;
}
In turn the function strcat_s
that should be renamed because there is standard function strcat_s
can look for example the following way
char * strcat_str( char *dest, const char *source )
{
size_t n = size_s( dest );
while ( ( *( dest n ) = *source ) != '\0' );
return dest;
}
CodePudding user response:
There are multiple issues in your code:
the
size_s
function really computes the size of the string, including the null terminator, but counting the null terminator is not helping for the task at hand, you should instead compute the length of the string, ie: the number of bytes before the null terminator, which is exactly the offset where to copy the second string at the end of the first.*(dest (index_of_dest 1)) = '\0';
does not store the null terminator at the correct place: it places it one step too far. You should write*(dest index_of_dest) = '\0';
or simplydest[ndex_of_dest] = '\0';
the name
strcat_s
may conflict with a library function of the same name defined in the infamous Annex K of the C Standard. A different name is preferable.scanf("%s", firstname);
is a security flaw: sufficient long input will cause a buffer overflow and carefully crafted input may allow the user to execute arbitrary code. Usescanf("