In the main method I declare a character pointer pCharacter. I pass this variable to the setCharacter method and assign it an address and a variable gets assigned to that address.
From what I understand, if I pass a pointer to a method, the method will manipulate the same address in memory as the method that called it. Even in the debugger when I hover over the pCharacter in the main method, I "see" that variable getting assigned an address and that address a value.
However when setCharacter() get closed, pCharacter in the main method is going to null, thus a segmentation fault when I try to print the character.
I was originally going to ask why this is happening, but now that I typed the question out in English I suspect it has something to do with a NULL pointer being passed from the main method so there is no common place in memory to be manipulated. Very strange since my debugger shows me the pCharacter variable changing in the main method when I hover over it. Is this just the debugger misleading me?
#include <stdio.h>
#include <stdlib.h>
void setCharacter(char* c);
void printCharacter(char* c);
int main (void)
{
char* pCharacter = NULL;
setCharacter(pCharacter);
printCharacter(pCharacter);
}
void setCharacter(char* pCharacter)
{
pCharacter = malloc(sizeof(char));
*pCharacter = 'b';
}
void printCharacter(char* pCharacter)
{
printf("%c", *pCharacter);
}