C program to copy one string to another using pointers


Explore how to efficiently copy one string to another using pointers in C programming. This article provides a comprehensive guide and complete code example, enabling programmers to enhance their understanding of string manipulation and pointer usage in C.

String manipulation is a fundamental aspect of programming, and C offers powerful tools for handling strings efficiently. Among these tools are pointers, which allow for dynamic memory management and precise control over string operations. In this article, we'll explore how to copy one string to another using pointers in C.

Understanding Pointers and Strings in C

Before diving into string copying using pointers, let's briefly review pointers and strings in C:

  • Pointers: Pointers are variables that store memory addresses. They provide a way to directly access and manipulate memory locations, making them essential for tasks like string manipulation.
  • Strings: In C, strings are represented as arrays of characters terminated by a null character ('\0'). They are typically stored in contiguous memory locations and are commonly manipulated using standard library functions or through manual pointer manipulation.

Copying Strings using Pointers

To copy one string to another using pointers in C, we'll iterate through each character of the source string, assigning it to the corresponding position in the destination string. Here's a step-by-step guide to copying strings using pointers:

  1. Declare Pointers: Define pointers to the source and destination strings.

    void stringCopy(char *dest, const char *src)
    {
        // Copy characters from src to dest
        while (*src != '\0') {
            *dest = *src;
            src++;
            dest++;
        }
        // Add null terminator to the destination string
        *dest = '\0';
    }
    
  2. Copy Characters: Iterate through each character of the source string and assign it to the destination string.

    // Copy characters from src to dest
    while (*src != '\0') {
        *dest = *src;
        src++;
        dest++;
    }
    
  3. Add Null Terminator: After copying all characters, add a null terminator ('\0') to the end of the destination string to mark the end of the string.

    // Add null terminator to the destination string
    *dest = '\0';
    

Write a C program to copy one string to another using pointers

Putting it all together, here's the complete C program to copy one string to another using pointers:

#include <stdio.h>

void stringCopy(char *dest, const char *src) {
    // Copy characters from src to dest
    while (*src != '\0') {
        *dest = *src;
        src++;
        dest++;
    }
    // Add null terminator to the destination string
    *dest = '\0';
}

int main() {
    char source[] = "Hello, world!";
    char destination[20]; // Make sure the destination array has enough space

    // Copy the string using pointers
    stringCopy(destination, source);

    printf("Original string: %s\n", source);
    printf("Copied string: %s", destination);

    return 0;
}

Output

Original string: Hello, world!
Copied string: Hello, world!

Here, we are passing the the reference of source and destination string, not just the value that's why actual string gets manipulated. Because pointers refer to the address of the start/first element for string and array. So, there is no need to pass by reference using &.

Arrays and String are always pass by reference in C.

stringCopy(destination, source);

But we will still get the same result even if we do so.

stringCopy(&destination, &source);

Recommended Posts