0

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);
}

1 Answer 1

2

Look at exactly what this code does:

void setCharacter(char* pCharacter)
{
     pCharacter = malloc(sizeof(char));
    *pCharacter = 'b';
}

Remember that things get passed by copy. First, pCharacter is an address that's passed from main to setCharacter. Then setCharacter changes that address by malloc'ing new memory and assigning the address of that memory to the local pCharacter. Note that it doesn't change the contents of pCharacter in main. That doesn't mean much here because the address passed in to setCharacter was NULL.

If you wanted to both allocate new memory and initialize the contents of that memory, it isn't necessary to pass in any variables here, but the process isn't complete.

The problem that you're having is this. While setCharacter is allocating new memory and initializing the contents (to 'b', in this case), it isn't returning any information to main. The address of the new memory is only known to setCharacter. You would need to return the address to main. So, depending on how much you would want setCharacter to do, there are two ways to do it. One just sets the contents, the other would both allocate the memory and set the value.

Method 1:

int main (void)
{
    char* pCharacter = malloc(sizeof(char));

    setCharacter(pCharacter);
    printCharacter(pCharacter);
}

void setCharacter(char* pCharacter)
{
    //this would only set the contents using the address from main
    *pCharacter = 'b';
}

Method 2:

int main (void)
{
    // the next line will create pCharacter pointer
    // and will initialize with the call to setCharacter
    char* pCharacter = setCharacter();
    printCharacter(pCharacter);
}

char * setCharacter(void)
{
     // this will both allocate memory and set the contents
     char *pCharacter = malloc(sizeof(char));
    *pCharacter = 'b';
     return pCharacter;
}

Note that you need to update the function signatures.

If this answers your questions, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • Thank you for the answer. I actually simplified my code a bit too much to make my question straightforward, what I was actually trying to do was change the address of a pointer after passing it to a function. Method 2 still works prefectly as a solution, but I also found: stackoverflow.com/questions/13431108/… however I found that syntax very hard to understand, and much easier to just return a value
    – BigTJ
    Jun 14, 2020 at 0:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .