String Copy Function Implementations
String manipulation is a fundamental skill in C programming. This article demonstrates several approaches to implementing string copying functions, from basic to optimized versions.
Basic String Copy Implementation
The following implementation demonstrates the core logic of copying characters from a source string to a destination buffer:
#include <stdio.h>
char* strCopy(char *destination, const char *source)
{
if (destination == NULL || source == NULL)
{
return NULL;
}
char *ptr = destination;
while (*source != '\0')
{
*destination = *source;
destination++;
source++;
}
*destination = '\0';
return ptr;
}
Optimized Version with Pointer Arithmetic
This version achieves the same result with more concise pointer operations:
char* strCopy(char *destination, const char *source)
{
if (destination == NULL || source == NULL)
{
return NULL;
}
char *ptr = destination;
while (*source != '\0')
{
*destination++ = *source++;
}
*destination = '\0';
return ptr;
}
Single-Line Implementation
This compact version combines assignment and condition checking in one expression:
char* strCopy(char *destination, const char *source)
{
if (destination == NULL || source == NULL)
{
return NULL;
}
char *ptr = destination;
while ((*destination++ = *source++) != '\0')
{
// Assignment with conditional check combined
}
return ptr;
}
Copying Specified Number of Characters
The strnCopy function copies a limited number of characters from the source string:
char* strnCopy(char *destination, const char *source, int maxCount)
{
if (destination == NULL || source == NULL)
{
return NULL;
}
char *ptr = destination;
while (*source != '\0' && maxCount > 0)
{
*destination++ = *source++;
maxCount--;
}
*destination = '\0';
if (maxCount > 0)
{
while (maxCount > 0)
{
maxCount--;
*destination++ = '\0';
}
}
return ptr;
}
Complete Example with Test Cases
#include <stdio.h>
char* strCopy(char *destination, const char *source);
char* strnCopy(char *destination, const char *source, int maxCount);
int main()
{
char buffer[128] = {'\0'};
const char *text = "Hello World";
strCopy(buffer, text);
puts(buffer);
strnCopy(buffer, text, 5);
puts(buffer);
return 0;
}
Using assert() for Debugging
The assert() macro from <assert.h> is a debugging tool that terminates program execution when a condtiion evaluates to false (zero). It accepts a single expression parameter:
#include <stdio.h>
#include <assert.h>
void assert(int expression);
When expression is false, assert() outputs the location of the failure and aborts the program. This is particularly useful for catching null pointer issues during development.
Enhanced Version with assert()
#include <stdio.h>
#include <assert.h>
char* strCopy(char *destination, const char *source)
{
assert(destination != NULL && source != NULL);
char *ptr = destination;
while (*source != '\0')
{
*destination++ = *source++;
}
*destination = '\0';
return ptr;
}
int main()
{
char buffer[128] = {'\0'};
const char *text = "Sample text";
const char *invalid = NULL;
// This will trigger an assertion failure
strCopy(invalid, text);
// Normal copy operation
strCopy(buffer, text);
puts(buffer);
return 0;
}
Key Points
Null Pointer Checks: Always validate pointer parameters before dereferencing them.
String Terminator: Remember to append the null character '\0' after copying characters.
Return Value: These functions return a pointer to the destination string, enabling function chaining.
assert() Behavior: The macro is disabled when NDEBUG is defined, making it suitable only for debugging builds rather than production code.