1

I am trying to figure out how to change the letter "j" to a "Y" using a pointer in the program below:

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

int main()
{

    char *buffer[] = {"ABCDEFGH", "ijklmnop", "QRSTUVWX"};

    printf("the value as %%s of buffer[0] is %s\n", buffer[0]);
    printf("the value as %%s of buffer[1] is %s\n", buffer[1]);
    printf("the value as %%s of buffer[2] is %s\n", buffer[2]);
    printf("the sizeof(buffer[2]) is %d\n", sizeof(buffer[2]));


    printf("the value as %%c of buffer[1][3] is %c\n", buffer[1][3]);
    printf("the value as %%c of buffer[2][3] is %c\n", buffer[2][3]);

    /*create a pointer to the pointer at buffer[1]*/
    char *second = buffer[1];

    /*change character at position 2 of bufffer[1] to a Y*/
    second++;
    second = "Y";

    printf("character at location in second is %c\n", *second);
    printf("character at location in second+2 is %c\n", second+2);

    printf("the values as %%c of second second+1 second+2 second+3 are %c %c %c %c\n",*(second),*(second+1),*(second+2),*(second+3));

    return(0);

}

How can I change a character located in an array of strings?

2
  • 1
    What happens when you run that code? Commented May 16, 2013 at 21:52
  • 4
    Trying to change string literals like that is undefined behaviour. Commented May 16, 2013 at 21:54

3 Answers 3

2

Attempting to write to string literals is undefined behaviour.

Change:

char *buffer[] = {"ABCDEFGH", "ijklmnop", "QRSTUVWX"};

to:

char buffer[][10] = {"ABCDEFGH", "ijklmnop", "QRSTUVWX"};
Sign up to request clarification or add additional context in comments.

Comments

2

Just a thing I noticed

char *second = buffer[1];

/*change character at position 2 of bufffer[1] to a Y*/
second++;
second = "Y";

You're trying to set to the string "Y" instead of to the char 'Y'.

I'd do something more like

*second = 'Y';

Comments

1

There's two things that are going to make this not work:

First, let's look at how you modify things:

second++;
second = "Y";

Think about what you are doing here: you are making second point to the string "Y". This is certainly not your intent. You want to change the second character of whatever string second points to... there are two ways to go about that:

/* make the second character of whatever string the variable second to into a 'Y' */
second++;
*second = 'Y';

Alternatively, you could use the more intuitive:

second[1] = 'Y';

Now, even with this fix, your code will attempt to modify string literals, which will result in undefined behavior. Consider your code:

char *buffer[] = {"ABCDEFGH", "ijklmnop", "QRSTUVWX"};

At this point, buffer[0], buffer[1] and buffer[2] point to memory that (may) be read-only. Whether it actually is or not is irrelevant though; you should certainly TREAT it as read-only. Therefore, the attempt to change anything would fail. To avoid that issue change things slightly:

char *buffer[3];

buffer[0] = strdup("ABCDEFGH");
buffer[1] = strdup("ijklmnop");
buffer[2] = strdup("QRSTUVWX");

And it goes without saying, but be sure to call free to release the strings pointed to by buffer[0], buffer[1] and buffer[2] when you're done with them!

1 Comment

Man... everyone else types so fast!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.