Fixed bug that missed the null terminator on the dest in x_strcpy

This commit is contained in:
xavi 2024-09-17 20:52:42 -07:00
parent 787bf40377
commit 4fb48e6912
2 changed files with 7 additions and 4 deletions

View File

@ -12,15 +12,15 @@ int x_strlen(char* str){
// copy one string to another destination // copy one string to another destination
// must include the size of the destination // must include the size of the destination
void x_strcpy(char* dest, char* src, const int size_dest){ // if greater than destination than stop and return 1
int x_strcpy(char* dest, char* src, const int size_dest){
char* dest_ptr = dest; char* dest_ptr = dest;
char* src_ptr = src; char* src_ptr = src;
int i = 0; int i = 0;
while(*src_ptr != '\0'){ while(*src_ptr != '\0'){
if (i >= size_dest){ if (i >= size_dest){
fprintf(stderr, "Error: x_strcpy\nSource string exeeds destination memory\n"); return 1;
exit(1);
} }
*dest_ptr = *src_ptr; *dest_ptr = *src_ptr;
dest_ptr++; dest_ptr++;
@ -28,6 +28,9 @@ void x_strcpy(char* dest, char* src, const int size_dest){
i++; i++;
} }
*dest_ptr = *src_ptr;
return 0;
} }
// compare two strings // compare two strings
// return 0 if equal to eachother // return 0 if equal to eachother

View File

@ -2,7 +2,7 @@
#define X_STRING_H #define X_STRING_H
int x_strlen(char*); int x_strlen(char*);
void x_strcpy(char*, char*, const int); int x_strcpy(char*, char*, const int);
int x_strcmp(const char*, const char*); int x_strcmp(const char*, const char*);
char* x_strconcat(char*, char*); char* x_strconcat(char*, char*);