JawiMmm
JawiMmm

Reputation: 61

How come pointers assigned to a string don't manipulate the original string as a whole?

Sample code is:

void main()
{
    char stringy[] = "I am so dumb what is wrong with me";
    char *pStringy = stringy;

    pStringy = "please be gentle";

    printf("%s", stringy);
}

Output is:

I am so dumb what is wrong with me

(This is probably really basic, but I don't understand, and couldn't find the question (probably because I just didn't know how to phrase it))

I'm wondering why the output isn't "please be gentle" when -after pStringy is initialized- pStringy and stringy share the same memory address.

In contrast, doing the exact same thing with single values (but not arrays) works just like I'd expect, and the original value in the original variable gets changed.

Upvotes: 1

Views: 97

Answers (6)

nicusor
nicusor

Reputation: 348

In C arrays and pointers are very similar (this is not completely true since there are some very small and obscure differences). When you write

char stringy[] = "I am so dumb what is wrong with me";

you could get the same result by writing

char * stringy = "I am so dumb what is wrong with me";

When you make the assignment

char *pStringy = stringy;

both pointers point to the same data. If it weren't string literals you would be able to modify them like this.

pStringy[3] = 'f';

But you can't since they are in fact string literals.

Edit: you can do that, my C knowledge is getting worse

The real problem is that you assign the pointer to something else so now it will point to a completely different value. You can test this with the following code:

void main()
{
char stringy[] = "I am so dumb what is wrong with me";
char *pStringy = stringy;

//print the addresses before assigning pStringy
printf("%d\n%d\n", stringy, pStringy);

pStringy = "please be gentle";

printf("%s\n%s\n", stringy, pStringy);

//print the addresses after assigning pStringy
printf("%d\n%d\n", stringy, pStringy);

}

Upvotes: 0

M.M
M.M

Reputation: 141618

Pointers point at other parts of memory. They don't contain or own the things they point at.

Here is a badly drawn MS paint sketch of your computer's memory Before and After the line:

pString = "please be gentle"

Pointer diagram

Hopefully this clears things up for you. As you can see, stringy remains unchanged.

Upvotes: 5

milevyo
milevyo

Reputation: 2180

char stringy[] = "I am so dumb what is wrong with me";
char *pStringy = stringy; //pStringy  point to stringy array

pStringy = "please be gentle";// instead of writing to stringy
                              // trough the pointer pStringy 
                              // you assigned pStringy a new address
                              // of the string "please be gentle"

printf("%s\n",stringy);       //printf will still print the old value
                              // "I am so dumb what is wrong with me"

                              // the proper way was to copy that string
                              // to the pointer using strcpy
                              // as so:
pStringy = stringy;
strcpy(pStringy , "please be gentle");

// now printf will print "please be gentle"
// instead of the old value
// "I am so dumb what is wrong with me"

printf("%s\n",stringy);
;

Upvotes: 1

Michi
Michi

Reputation: 5297

Because you print stringy instead of pStringy. Anyway try this:

#include<stdio.h>

int main(void)    {
    char stringy[] = "I am so dumb what is wrong with me";
    char *pStringy = stringy;

    pStringy = "please be gentle";
    printf("%s\n%s",stringy, pStringy);

    return 0;
}

Output:

I am so dumb what is wrong with me
please be gentle

You should know that main should be at least int main(void){}

EDIT:

There some things which you have to undestand.

1) pString point to stringy.

2) latter pString point to another memory location (read only).

3) So at this point pStringy doesn't point to stringy any more, which means, that, there is no modified Array as you expected.

Upvotes: 2

Weather Vane
Weather Vane

Reputation: 34585

You set pStringy to point to the first message with

char *pStringy = stringy;

Then you overwrite that pointer with another message

pStringy = "please be gentle";

but you completely ignore that, and print the original message with

printf("%s", stringy);

so it prints

I am so dumb what is wrong with me

Upvotes: 0

Abr001am
Abr001am

Reputation: 591

this is what your compiler did.

char *pStringy = stringy;

pStringy = "please be gentle";

assigning a pointer to a value, then assigning same poiter to another value, is always yielding to the last value assigned.

Upvotes: 0

Related Questions