How to Use Pointers and Strings in C Programming

By Dan Gookin

The C programming language lacks a string variable, but it does have the char array, which is effectively the same thing. As an array, a string in C can be completely twisted, torqued, and abused by using pointers. It’s a much more interesting topic than messing with numeric arrays.

How to use pointers to display a string

You’re most likely familiar with displaying a string in C, probably by using either the puts() or printf() function. Strings, too, can be displayed one character a time by plodding through an array.


#include <stdio.h>
int main()
 char sample[] = "From whence cometh my help?n";
 int index = 0;
 while(sample[index] != '')

The code shown in Hello, String is completely legitimate C code, valid to create a program that displays a string. But it doesn’t use pointers, does it?

Exercise 1: Modify the source code from Hello, String, replacing array notation with pointer notation. Eliminate the index variable. You need to create and initialize a pointer variable.

The while loop’s evaluation in Hello, String is redundant. The null character evaluates as false. So the evaluation could be rewritten as


As long as the array element referenced by sample[index] isn’t a null character, the loop spins.

Exercise 2: Edit the while loop’s evaluation in your solution for Exercise 1, eliminating the redundant comparison.

Exercise 3: Continue working on your code, and this time eliminate the statements in the while loop. Place all the action in the while statement’s evaluation. For the sake of reference, the putchar() function returns the character that’s displayed.

How to declare a string by using a pointer

Here’s a scary trick you can pull using pointers, one that comes with a boatload of caution. Consider A Pointer Announces a String.


#include <stdio.h>
int main()
 char *sample = "From whence cometh my help?n";

In A Pointer Announces a String, the string that’s displayed is created by initializing a pointer. It’s a construct that looks odd, but it’s something you witness often in C code, particularly with strings. (You cannot use this convention to initialize a numeric array.)

Exercise 4: Copy the source code from A Pointer Announces a String in your editor. Build and run.

The boatload of caution in A Pointer Announces a String, and anytime you use a pointer to directly declare a string, is that the pointer variable can’t be manipulated or else the string is lost.

For example, in A Pointer Announces a String, the sample variable is used in Line 7 to step through the string as part of the putchar() function. Oops. If you wantto use sample later in the code, it would no longer reference the start of the string.

When declaring a string by using a pointer, don’t mess with the pointer variable elsewhere in the code.

The solution is to save the pointer’s initial address or simply use a second pointer to work on the string.

Exercise 5: Fix the code in A Pointer Announces a String so that the sample variable’s value is saved before the while loop runs and is then restored afterward. Add a puts(sample) statement to the code after the while loop is done executing, to prove that the variable’s original address is restored.

How to sort strings

Taking what you know about sorting in the C language, you can probably craft a decent string-sorting program. Or, at minimum, you can explain how it’s done. That’s great! But it’s a lot of work.

What’s better when it comes to sorting strings is not to sort the strings at all. No, instead, you sort an array of pointers referencing the strings. Sorting Strings, Initial Attempt shows an example.


#include <stdio.h>
int main()
 char *fruit[] = {
 char *temp;
 int a,b,x;
 if(*(fruit+a) > *(fruit+b))
 temp = *(fruit+a);
 *(fruit+a) = *(fruit+b);
 *(fruit+b) = temp;

Exercise 6: Type the source code from Sorting Strings, Initial Attempt into your editor. Build and run to ensure that the strings are properly sorted.

Well, it probably didn’t work. It may have, but if the list is sorted or changed in any way, it’s an unintended consequence and definitely not repeatable.

The problem is in Line 19. You can’t compare strings by using the > operator. You can compare individual characters and you could then sort the list based on those characters, but most humans prefer words sorted across their entire length, not just the first character.

Exercise 7: Modify your source code, and use the strcmp() function to compare strings to determine whether they need to be swapped.