Declaring a String Literal as a Pointer

The best way to declare a string literal in your code is to use array notation, like this:

char string[] = "I am some sort of interesting string.\n";

This type of declaration is 100 percent okey-doke. The compiler allocates proper storage for the string, including the null character at the end, and it assigns it to the array string. Nifty keen.

You can also declare a string in this manner:

char *string = "I am some sort of interesting string.\n";

Again, in the code you can use the string variable to reference the text — but that’s about it. Altering the string or passing it to a function renders heaps of trouble. So, while you effectively create a string, you pretty much doom your code not to do anything with the string. In fact, there’s no clear advantage to such a declaration other than it frightens C programmers who don’t like pointers.

Recently, I researched this phenomenon, trying to discover if I could possibly find a way around the pointer-string declaration limitation. First, I concocted a program using array notation, but manipulating the string in a function as a pointer:

#include <stdio.h>

void change(char *s)
{
    *(s+1) = 'i';
}

void output(char *s)
{
    printf("You are %s\n",s);
}

int main()
{
    char string[] = "fat";

    output(string);
    change(string);
    output(string);

    return(0);
}

In the change() function, the middle character of fat is changed to an i. Here’s the output:

You are fat
You are fit

If you change the declaration in the main() function to read char *string = "fat"; the program won’t run. It compiles, but the output chokes after the first string is displayed.

I tried everything to get the program to run, but failed. The proper address is passed to the change() function, however any attempt to modify the string causes the program to crash with a memory violation. This situation is frustrating, especially because you can pass the string to the output() function without a hitch.

The only way I can get the change() function to modify the string pointer is when it’s not initialized. In the following code, the pointer is declared, allocated memory, then a string is assigned. After that, the program runs without a memory violation:

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

void change(char *s)
{
    *(s+1) = 'i';
}

void output(char *s)
{
    printf("You are %s\n",s);
}

int main()
{
    char *string;

    string= (char *)malloc( sizeof(char)*4 );
    if( string == NULL )
    {
        puts("Unable to allocate string storage");
        exit(1);
    }
    strcpy(string,"fat");

    output(string);
    change(string);
    output(string);

    return(0);
}

I would think that this code would run the same if I merely assigned *string to a literal, but it doesn’t. And, yes, I tried passing pointers-to-pointers and other tricks. Apparently the memory allocated by a pointer-string declaration is somehow restricted. That’s perfectly okay with me: You have plenty of alternatives for allocating string literals. And using array notation to address the issue is an easy fix.

Leave a Reply