C program demonstrating the concepts of strings using Pointers

In C programming, strings can be effectively manipulated using pointers. A string is essentially an array of characters terminated by a null character ('\0'). When we use pointers with strings, we can access and manipulate individual characters efficiently.

Syntax

char *pointer_name = "string_literal";
char *array_of_pointers[] = {"string1", "string2", "string3"};

String Declaration and Initialization

Strings can be declared and initialized in multiple ways −

  • Using character array: char string[50];
  • Using character constants: char string[10] = {'H', 'e', 'l', 'l', 'o', '\0'};
  • Using string literals: char string[10] = "Hello";
  • Using pointer to string: char *string = "Hello";

Arrays of Pointers to Strings

An array of pointers to strings is an efficient way to store multiple strings. Each element points to the base address of a different string −

char *names[] = {"Alice", "Bob", "Charlie"};

Here, names[0] points to "Alice", names[1] points to "Bob", and names[2] points to "Charlie".

Example 1: Basic String Pointer Operations

This example demonstrates basic string pointer operations and character access −

#include <stdio.h>

int main() {
    char *s = "TutorialsPoint";
    
    printf("Complete string: %s<br>", s);
    printf("First character: %c<br>", *s);
    printf("Fifth character: %c<br>", *(s+4));
    printf("Character at index 8: %c<br>", *(s+8));
    
    return 0;
}
Complete string: TutorialsPoint
First character: T
Fifth character: r
Character at index 8: P

Example 2: Pointer Arithmetic with Strings

This example shows how pointer arithmetic works with string manipulation −

#include <stdio.h>

int main() {
    char *s = "Programming";
    char *ptr = s;
    
    printf("Original string: %s<br>", s);
    printf("Character at ptr: %c<br>", *ptr);
    
    ptr = ptr + 3;
    printf("After ptr+3: %c<br>", *ptr);
    printf("Substring from ptr: %s<br>", ptr);
    
    return 0;
}
Original string: Programming
Character at ptr: P
After ptr+3: g
Substring from ptr: gramming

Example 3: Array of String Pointers

This example demonstrates how to use an array of pointers to store and access multiple strings −

#include <stdio.h>

int main() {
    char *languages[] = {"C", "Java", "Python", "JavaScript"};
    int count = 4;
    
    printf("Programming Languages:<br>");
    for(int i = 0; i < count; i++) {
        printf("%d. %s<br>", i+1, languages[i]);
    }
    
    printf("\nFirst character of each language:<br>");
    for(int i = 0; i < count; i++) {
        printf("%s starts with: %c<br>", languages[i], *languages[i]);
    }
    
    return 0;
}
Programming Languages:
1. C
2. Java
3. Python
4. JavaScript

First character of each language:
C starts with: C
Java starts with: J
Python starts with: P
JavaScript starts with: J

Key Points

  • String pointers provide efficient access to string data without copying entire strings.
  • Pointer arithmetic allows navigation through string characters using *(ptr + index).
  • Arrays of string pointers are memory-efficient for storing multiple strings.
  • Always ensure proper null termination when working with string pointers.

Conclusion

String manipulation using pointers in C provides powerful and efficient ways to handle text data. Understanding pointer arithmetic and string addressing is fundamental for effective C programming and memory management.

Updated on: 2026-03-15T13:24:41+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements