Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

String Pointers in C

Introduction

Pointers are a powerful feature of the C programming language. They allow for efficient handling of data and memory. When it comes to strings, pointers can be especially useful. This tutorial will cover the concept of string pointers in C, providing detailed explanations and examples.

What is a String?

In C, a string is essentially an array of characters terminated by a null character '\0'. Strings can be defined in two main ways:

Using an array:

char str[] = "Hello, World!";

Using a pointer:

char *str = "Hello, World!";

While both methods are used to store strings, they have different implications when used with pointers.

Pointer to a String

When you use a pointer to point to a string, you are actually pointing to the first character of that string. Consider the following example:

#include <stdio.h>

int main() {
    char *str = "Hello, World!";
    printf("%s\n", str);
    return 0;
}
                

Here, str is a pointer to the first character of the string "Hello, World!". The printf function prints the string until it encounters the null character.

Accessing String Characters via Pointers

You can access individual characters of a string using pointers. Consider the following example:

#include <stdio.h>

int main() {
    char *str = "Hello, World!";
    printf("First character: %c\n", *str);
    printf("Second character: %c\n", *(str + 1));
    return 0;
}
                

In this example, *str gives the first character 'H', and *(str + 1) gives the second character 'e'.

Modifying Strings using Pointers

Strings declared as arrays can be modified, but strings declared as pointers to string literals cannot be modified directly. Observe the following example:

#include <stdio.h>

int main() {
    char str[] = "Hello, World!";
    str[0] = 'h';
    printf("%s\n", str);
    return 0;
}
                

This will change the first character to 'h' and print "hello, World!". However, the following code will cause undefined behavior:

#include <stdio.h>

int main() {
    char *str = "Hello, World!";
    str[0] = 'h'; // This is undefined behavior!
    printf("%s\n", str);
    return 0;
}
                

String Manipulation Functions

C provides several standard library functions for string manipulation. Some of these include:

  • strlen(): Calculates the length of a string.
  • strcpy(): Copies one string to another.
  • strcat(): Concatenates two strings.
  • strcmp(): Compares two strings.

Here's an example of using strlen() and strcpy() with string pointers:

#include <stdio.h>
#include <string.h>

int main() {
    char *str = "Hello, World!";
    char buffer[50];
    
    printf("Length of string: %lu\n", strlen(str));
    strcpy(buffer, str);
    printf("Copied string: %s\n", buffer);
    
    return 0;
}
                

Conclusion

String pointers in C provide a powerful way to handle strings. They offer flexibility and efficiency, but also come with potential pitfalls, especially regarding string literals and memory management. Understanding how to use string pointers effectively is crucial for any C programmer.

We hope this tutorial has provided a clear and comprehensive overview of string pointers in C. Happy coding!