0

I am trying to create a function that would allow me ask a user for files. When I tried printing the addresses using "%p", fileGot, Buffer and *userFileNameInput all have the same address stored. However, when I try to verify the value in the main function, the pointer is NULL. Can someone help explain why this is, and what would be a better way to go about it?

int main()
{

    char *fileGot;
    fileGetter( &fileGot );
    printf("filegot: %s\n" , fileGot);

    return 0;
}

void fileGetter( char **userFileNameInput )
{

    char buffer[100];
    *userFileNameInput = buffer;
    printf("enter file: \n");
    scanf("%s" , buffer);
}
kmdreko
  • 42,554
  • 6
  • 57
  • 106
  • `buffer` stops existing when `fileGetter` returns, you cannot try to access its contents after that. You could declare `buffer` in `main` and pass pointer to first element, plus the buffer size – M.M Jul 13 '18 at 02:59
  • The stack entry is cleared. You aren't allocating heap memory for the array so it isn't being passed back. – Mad Physicist Jul 13 '18 at 03:00

0 Answers0