Manipulating a String in a Function

You have two choices when modifying a string in a function: You can modify the string directly or you can create a new string and return it. Either way, a new string is created based on the old.

If the function returns a new string, ensure that the string storage is classified as static. Even if you allocate storage in the function, ensure that the pointer is static. Otherwise, when the function terminates, the string is discarded. But for my project, I choose to modify a string without having to return it.

To manipulate a string in a function, you reference its memory location and access the characters via pointers. You could fake this process using array notation, but remember my desire is to pass several strings strings in an array.

As with any programming project, I start out simple.

2021_08_28-Lesson-a.c

#include <stdio.h>

void increase(char *a)
{
    while(*a)
    {
        *a = *a + 1;
        a++;
    }
}

int main()
{
    char a[] = "Hello, world!";

    printf("%s = ",a);
    increase(a);
    printf("%s\n",a);

    return(0);
}

The main() function declares a single string. It’s output at Line 16, sent to the increase() function for modification at Line 17, and the updated string is output at Line 18.

The increase() function takes a char pointer as its argument, a string. The string is modified within the function, each character value incremented by one. Nothing is returned because pointer allows the string to be manipulated directly in memory.

Here’s output:

Hello, world! = Ifmmp-!xpsme"

So far, so good. The string is modified directly within the increase() function. The next step is to upgrade the code to process an array of strings:

2021_08_28-Lesson-b.c

#include <stdio.h>

void increase(char *a)
{
    while(*a)
    {
        *a = *a + 1;
        a++;
    }
}

int main()
{
    char *strings[3] = {
        "Hello, world!",
        "This is a string test",
        "Once upon a time, there was a string"
    };
    int x;

    for( x=0; x<3; x++ )
    {
        printf("%s = ",strings[x]);
        increase(strings[x]);
        printf("%s\n",strings[x]);
    }

    return(0);
}

The only change here is the addition of the strings[] array, which contains three strings. Remember, this is an array of pointers, a ** thing.

Each string in the array is output, modified within the increase() function, then output again.

Here’s a sample run:

Segmentation fault (core dumped)

I spent Way Too Much Time trying to figure out a solution. I tried sending a pointer to the string instead of the string itself:

increase(&strings[x]);

This modification required I update the function’s definition to:

void increase(char **a)

Further modifications were required to address individual characters. The format looks like this *(*a+x), but I need not get into it because . . . and this was my eventual revelation . . . it was never going to work.

I explore why it will never work in next week’s Lesson. To tide you over, consider reviewing this blog post on the pointer-pointer thing. It’s quite immersive, though it doesn’t actually address my current problem.

Leave a Reply