Page Content

Posts

Copy String Using Pointers In C Programming Language

Copy String Using Pointers in C

A collection or sequence of characters stored in an array of type character is the basic definition of a string in C. The fact that strings in C end with the special character \0, or NULL, is one of their most important features. The compiler interprets this null character as the string’s end. The C compiler automatically adds the \0 at the end when you declare and initialize a string variable using an assignment operator with a collection of characters enclosed in double quotation marks, such as char state [ ] = “Gujarat”;.

Additionally, the NULL character is automatically attached to the end of string constants groups of letters surrounded in double quotes, such as “Hello World” or “A” when they are stored. One character, the \0, is present in even a null string “”.

In C, the ideas of arrays, strings, and pointers are closely connected. In C, a pointer to the string’s initial character is used to access the string. A string’s “value” is regarded as the address of its first character.

In C, a character array can be declared in order to declare a string variable. For instance, declaring an array s that can contain a string is done with char s[MAXSTRING];. It’s crucial to make sure a character array is big enough to hold the string’s entire character count as well as its null character at the end.

Pointer variables can also be used to manage strings. The address of another variable is stored in a special variable called a pointer. To point to strings, one usually uses a char * pointer. By declaring a pointer p of type “pointer to char” and initialising it with the beginning location of the string literal “Programming is my Passion” that is stored in memory, for example, char *p = “Programming is my Passion.” This declaration returns the string’s beginning address to pointer p and stores it in memory.

Character arrays and char * pointers are used slightly differently for strings.

  • String characters are typically changeable, and character arrays like char str[] = “Hello”; or char s[MAXSTRING]; allot space for the string characters directly in the array where they are declared. Its value (the array’s base address) cannot be altered; the array name itself (str or s) is regarded as a constant pointer that links to the array’s first element.
  • Character pointers, such as char *p = “Hello”; are frequently used to access string literals that may be kept in a read-only memory region (such as the.rdata section with Visual C++ /GF switch). Usually, you can’t change the content of the string literal they point to. The pointer variable itself (p), on the other hand, is a variable pointer that can be altered to point to a different string or location in memory. A permitted operation is to assign a string literal to a char * pointer variable, such as p = “Bye”;.

String characters can be processed using pointers. By increasing the pointer that links to the first letter in a string, you can access successive characters since array elements are kept in contiguous memory locations. An illustration of pointer arithmetic is this. A pointer that has been incremented points to the next position of its type. Incrementing a char * pointer advances it to the following character, which is usually one byte away.

By dereferencing the pointer, you can use pointer notation to access specific characters within a string. *p provides you with the first character if p is a char * pointer pointing to the start of a string. The pointer p++ points to the following character when it is incremented. By continuously accessing *p and increasing p until you hit the null character \0, you can iterate across the string.

To access string characters, you can alternatively use pointer/subscript notation. p[i] is the same as *(p + i) if p is a reference to the beginning of a string. This allows you to access the character at index i in relation to the pointer’s starting place. This enables you to use an index to read characters without altering the address of the reference.

Pointers are frequently used as parameters in string-handling functions. Const char * is a common parameter type for functions that handle input strings that shouldn’t be changed. This means that even though the pointer itself might be changed within the function’s scope to point somewhere else (unless it is also qualified with const), the function will not change the string the pointer points to.

Example: The following code sample shows how to utilize pointers to copy a string:



#include <stdio.h>
#include <string.h> // For strlen

void copy_string(char*, char*);

int main() // main should return int
{
    char source[100], target[100];
    printf("Enter source string\n");
    fgets(source, sizeof(source), stdin); // Use fgets instead of gets

    // Remove the newline character if fgets read one
    source[strcspn(source, "\n")] = 0;

    copy_string(target, source);
    printf("Target string is \"%s\"\n", target);
    return 0;
}

void copy_string(char *target, char *source)
{
    while(*source)
    {
        *target = *source;
        source++;
        target++;
    }
    *target = '\0';
}

Output:

Enter source string
Govindhtech Solutions
Target string is "Govindhtech Solutions"

This example shows how to traverse and edit the characters of the strings directly in memory using pointer variables (to and from). Pointer arithmetic (moving the pointer to the next character) is done with the ++ operator, whereas dereferencing (accessing the character at the current pointer location) is done with the * operator. The loop keeps going until the null character \0 is copied, which indicates that the copied string is finished. This demonstrates how pointers and character arrays are closely related when working with strings in C.

You can also read What Are Function Pointers In C With Code Example?

Agarapu Geetha
Agarapu Geetha
My name is Agarapu Geetha, a B.Com graduate with a strong passion for technology and innovation. I work as a content writer at Govindhtech, where I dedicate myself to exploring and publishing the latest updates in the world of tech.