Nick
Nick

Reputation: 958

Having a function change the value a pointer represents in C

I have a main function that has a char, I am attempting to pass a pointer to that char into a function and have it change it from A to B but it just doesn't seem to change it. The example shown here is just the current state of the code I have tried many different variations on it thus there may be other mistakes in there from simply clutching at straws.

int main()
{
    char result = 'A';
    setChar(&result);
    printf("%C", result);
}

void setChar(char* charToChange)
{
    charToChange = "B";
}

Upvotes: 27

Views: 129802

Answers (7)

R242
R242

Reputation: 1

#include <stdio.h>

void x(char* a){
    *a = *"c";  // here variable a value change into "b"
}
int main()
{
    char a = 'a';
    x(&a);
    printf("%c\n",a);
    return 0;
}

Upvotes: 0

Chris Reid
Chris Reid

Reputation: 488

C copies pointers that are passed in to functions as parameters. So inside the function you are manipulating a copy of the pointer, not the pointer itself. When the function exits, the pointer if it was changed as in

 while (p++)  if (*p = '\0') break;  //example

will return to its previous value and the copy will be destroyed. The object of memory location the pointer points to may be changed and that is the whole idea behind pass by reference. A common mistake is trying to null a pointer inside of a function and then discovering it not being null upon return from the function. On some systems you get back the pointer pointing to garbage or bad memory locations that crash your program when you try to read from or write to the variable.

void f(char* s)
{
  /* code stuff */
...
s = NULL;
...
return;
}

upon return from f now s = ? (previous value , NULL, or GARBAGE ) This happens most often with variables passed to functions defined in separate modules that take values by reference, or across execution contexts (like threads or shared memory between processes).

Upvotes: 1

Sahil Doshi
Sahil Doshi

Reputation: 1171

when you call setChar function with &result as parameter ,it passes Address of result where it is stored.

so it gets assigned to char pointer * charToChange

Let say Address of result is [0x00000010] -> 'A'

And Address of charToChange is [0x00000100] -> 0x00000010

Now When you try to write charToChange = "B";

It Creates New Memory where it stores "B"

Let Say [0x00001000] -> 'B' && [0x00001001] -> '\0'

So While doing Assignment it stores

[0x00000100] -> 0x00001000

But Yet The Address 0x00000010 is Pointing to A

So it is Wrong

You should replace charToChange = "B"; to

*charToChange = 'B';

So that Value at 0x00000100 Becomes 'B'

Always Remember

* Means ValueAt And

& Means AddressOf

Upvotes: 11

Dario
Dario

Reputation: 49218

You want to change the value the pointer points to, not the pointer itself.

Thus you need to dereference the pointer with *pointer:

void setChar(char* charToChange) {
    *charToChange = 'B';
}

If you don't, you just change the local value of charToChange.

Upvotes: 5

Dave
Dave

Reputation: 3448

You need to dereference it, and the literal must be a char rather than a string, i.e. use apostrophes rather than double quotes:

void setChar(char* charToChange)
{
    *charToChange = 'B';
}

Upvotes: 4

Fr&#233;d&#233;ric Hamidi
Fr&#233;d&#233;ric Hamidi

Reputation: 262989

You have to dereference the pointer passed to setChar() in order to modify the value it points to, not the pointer argument itself.

You also have to use the character literal 'B' instead of the string literal "B" (which is a pointer to char, not a char).

void setChar(char* charToChange)
{
    *charToChange = 'B';
}

Upvotes: 15

Jeremiah Willcock
Jeremiah Willcock

Reputation: 30969

What you want is *charToChange = 'b';. The pointer charToChange is a local variable (parameter) in setChar, but you can change what it points to using the prefix * operator and an assignment. Note that *charToChange is a character, too, not a string.

Upvotes: 40

Related Questions