27

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";
}
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Nick
  • 948
  • 2
  • 12
  • 24
  • Please use the formatting features provided by [Markdown](http://stackoverflow.com/editing-help) where possible, especially for code fragments. The easy way to do this is to format the code neatly in an external editor, copy and paste to the question, select the code and click the `{}` button at the top of the edit box. This inserts four spaces at the beginning of each line. – RBerteig Jan 30 '11 at 20:09
  • Clearer thinking would have let you find the answer yourself much more quickly. Pointers don't "represent" values; they **point to** values (hence the name). You want to assign to the value that the pointer points at. You get the pointed-at value by dereferencing the pointer. You want to assign a character. You specify a character with single quotes. – Karl Knechtel Jan 30 '11 at 22:51
  • Thanks, I understand the concept just fine, it was the syntax that was the issue. – Nick Feb 01 '11 at 11:14
  • Related, possible duplicate https://stackoverflow.com/questions/2229498/passing-by-reference-in-c – TylerH Oct 11 '22 at 13:47

7 Answers7

39

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.

Jeremiah Willcock
  • 30,161
  • 7
  • 76
  • 78
  • 2
    Thank you, you wouldn't believe how many hours I have thrown away over that little *. Much appreciated. – Nick Jan 30 '11 at 19:27
  • 3
    @ Nick Might want to consider getting a better compiler. Most compilers give a warning for implicit typecasts like this. – Lundin Jan 30 '11 at 20:14
15

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';
}
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
11

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

Sahil Doshi
  • 1,073
  • 10
  • 23
5

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.

Dario
  • 48,658
  • 8
  • 97
  • 130
4

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';
}
Dave
  • 3,438
  • 20
  • 13
1

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).

Chris Reid
  • 460
  • 4
  • 9
0
#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;
}
R242
  • 1
  • 1
  • Please don't post only code as answer, but also provide an explanation of what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality. – blackbishop Dec 21 '21 at 12:43