Pointers with Strings in C

Hello, fellow coders! Today, we’re going to dive into the world of pointers and strings in C. If you’ve ever wondered how pointers work with strings in C, or how to create and manipulate strings using pointers, you’re in the right place. So, let’s get started!

Understanding Pointers and Strings

In C, strings are essentially arrays of characters, terminated by a null character ('\0'). A pointer to a string is a pointer to its first character. For instance, if we have a string char Name[] = "Dinesh";, we can create a pointer char *ptrname; and assign the base address of Name to ptrname like this: ptrname = Name;.

Working with Pointers and Strings

Let’s see how we can work with pointers and strings in C. Consider the following code:

#include<stdio.h>

int main() {
    char Name[] = "Dinesh";
    char *ptrname;
    ptrname = Name;
    printf("%s", ptrname);
    return 0;
}
C

In this code, ptrname is a pointer to the string Name. When we print ptrname, it outputs Dinesh, the string that ptrname points to.

Array of Pointers to Strings

An array of pointers to strings is an array where each element is a pointer to a string. Here’s how you can declare and initialize an array of pointers to strings:

char *sports[5] = {"Football", "Basketball", "Cricket", "Tennis", "Soccer"};

In this array, sports[0] is a pointer to the base address of the string "Football", sports[1] is a pointer to the base address of the string "Basketball", and so on.

Manipulating Strings Using Pointers

One of the advantages of using pointers with strings is that you can manipulate the characters within a string. However, it’s important to note that you can’t directly modify the string pointer itself. If you try to do so, the compiler will produce a runtime error.

Code Examples

Let’s look at some examples of how to use pointers with strings in C.

Example 1: Printing Strings Using Pointers

#include<stdio.h>

int main() {
    char *a[5] = {"one", "two", "three", "four", "five"};
    int i;
    printf("The strings are: ");
    for (i=0; i<5; i++)
        printf("%s ", a[i]);
    return 0;
}
C

Output: The strings are: one two three four five

Example 2: Manipulating Characters of a String Using Pointers

#include<stdio.h>

int main() {
    char str[] = "Hello";
    char *ptr = str;
    *(ptr+1) = 'a';
    printf("%s", str);
    return 0;
}
C

Output: Hallo

Wrapping Up

Pointers and strings in C can be a bit tricky at first, but with practice, you’ll get the hang of it. Remember, a pointer to a string is just a pointer to its first character, and you can use pointers to manipulate the characters within a string.

Frequently Asked Questions (FAQ)

  1. How do pointers work with strings in C?

    A pointer to a string in C is a pointer to its first character.

  2. Can pointers store strings?

    Pointers can store the address of the first character of a string, effectively giving you access to the entire string.

  3. How to make a string pointer array in C?

    An array of pointers to strings in C can be created by declaring an array where each element is a pointer to a string.

  4. How to initialize a string in C using pointers?

    A string can be initialized in C using pointers by assigning the address of the first character of the string to the pointer.

  5. Can we modify the string using pointers in C?

    Yes, we can modify the characters within a string using pointers. However, we cannot directly modify the string pointer itself.

  6. What is the difference between a string and a pointer to a string in C?

    A string in C is an array of characters, while a pointer to a string is a pointer to the first character of that string.

  7. How does pointer arithmetic work with strings in C?

    Pointer arithmetic with strings works as usual. If you increment

If you found this tutorial helpful, you might also be interested in the following topics:

Dr. Mehedi Hasan is a seasoned semiconductor professional, academic and web-designer with over a decade of experience in digital system design and verification as well as web development. Currently a Senior Engineer at AMD in Markham, Ontario, he plays a key role in the development and verification of cutting-edge chip technologies, earning multiple Spotlight Awards for his contributions.

Dr. Hasan holds a Ph.D. in Electrical and Computer Engineering from the University of Saskatchewan and has served in both academia and industry across Canada, Bangladesh, Malaysia, and Saudi Arabia. His expertise spans web-development, UVM-based SystemVerilog verification, static timing analysis (STA), RTL design, and scripting in multiple languages including Python, TCL, Shell as well as web-development tools including HTML, CSS, Javascript.

Passionate about knowledge sharing and education, Dr. Hasan has also worked as an Assistant Professor in Ontario, Canada (at Lakehead University) and Bangladesh University. He is committed to building accessible learning environments and is the founder of SkillSeminary, a platform focused on simplifying complex tech concepts for learners worldwide.

When he's not immersed in chip verification or educational projects, Dr. Hasan enjoys mentoring, researching system development, and promoting open tech education.

Scroll to Top