Skip to main content
deleted 2 characters in body
Source Link
user17592432
user17592432

Having tried to guide toward understanding this problem, it seems best to present what should be working code (for study.) Sometimes too many words merely muddle the situation:

char *my_strcat(char *restrict dest, const char *restrict src, size_t d_size) { if( !dest || !src ) return NULL; size_t src_len = strlen( src ); size_t dest_len = strlen( dest ); if( dest_len + src_len + 1 > d_size ) return NULL; char *p = dest + dest_len; while( (*p++ = *src++ ) != '\0' ) ; return dest; } int main() { const char *src = "Hello"; char dest[100] = " world!"; printf("Src : %s Dest : %s\n", src, dest); char *test = my_strcat( dest, src, sizeof dest ); if( test ) printf("Value : %s\n", test ); return 0; } 

Now, one can experiment by shrinking the size of dest to something larger than " world!" but smaller than " world!Hello"... Perhaps 9 bytes???

And, now that the concatenation should be working (into a big enough buffer), adding the code to ensure there is no overlap of the actual character arrays. Known is the lengthsize of dest, and the length of src is measured.

Having tried to guide toward understanding this problem, it seems best to present what should be working code (for study.) Sometimes too many words merely muddle the situation:

char *my_strcat(char *restrict dest, const char *restrict src, size_t d_size) { if( !dest || !src ) return NULL; size_t src_len = strlen( src ); size_t dest_len = strlen( dest ); if( dest_len + src_len + 1 > d_size ) return NULL; char *p = dest + dest_len; while( (*p++ = *src++ ) != '\0' ) ; return dest; } int main() { const char *src = "Hello"; char dest[100] = " world!"; printf("Src : %s Dest : %s\n", src, dest); char *test = my_strcat( dest, src, sizeof dest ); if( test ) printf("Value : %s\n", test ); return 0; } 

Now, one can experiment by shrinking the size of dest to something larger than " world!" but smaller than " world!Hello"... Perhaps 9 bytes???

And, now that the concatenation should be working (into a big enough buffer), adding the code to ensure there is no overlap of the actual character arrays. Known is the length of dest, and the length of src is measured.

Having tried to guide toward understanding this problem, it seems best to present what should be working code (for study.) Sometimes too many words merely muddle the situation:

char *my_strcat(char *restrict dest, const char *restrict src, size_t d_size) { if( !dest || !src ) return NULL; size_t src_len = strlen( src ); size_t dest_len = strlen( dest ); if( dest_len + src_len + 1 > d_size ) return NULL; char *p = dest + dest_len; while( (*p++ = *src++ ) != '\0' ) ; return dest; } int main() { const char *src = "Hello"; char dest[100] = " world!"; printf("Src : %s Dest : %s\n", src, dest); char *test = my_strcat( dest, src, sizeof dest ); if( test ) printf("Value : %s\n", test ); return 0; } 

Now, one can experiment by shrinking the size of dest to something larger than " world!" but smaller than " world!Hello"... Perhaps 9 bytes???

And, now that the concatenation should be working (into a big enough buffer), adding the code to ensure there is no overlap of the actual character arrays. Known is the size of dest, and the length of src is measured.

Source Link
user17592432
user17592432

Having tried to guide toward understanding this problem, it seems best to present what should be working code (for study.) Sometimes too many words merely muddle the situation:

char *my_strcat(char *restrict dest, const char *restrict src, size_t d_size) { if( !dest || !src ) return NULL; size_t src_len = strlen( src ); size_t dest_len = strlen( dest ); if( dest_len + src_len + 1 > d_size ) return NULL; char *p = dest + dest_len; while( (*p++ = *src++ ) != '\0' ) ; return dest; } int main() { const char *src = "Hello"; char dest[100] = " world!"; printf("Src : %s Dest : %s\n", src, dest); char *test = my_strcat( dest, src, sizeof dest ); if( test ) printf("Value : %s\n", test ); return 0; } 

Now, one can experiment by shrinking the size of dest to something larger than " world!" but smaller than " world!Hello"... Perhaps 9 bytes???

And, now that the concatenation should be working (into a big enough buffer), adding the code to ensure there is no overlap of the actual character arrays. Known is the length of dest, and the length of src is measured.