[Edit: Apperently the return value obtained from fgets is simply the address of the char buffer[SIZE] being used, so in my case it makes sense because I declared it locally (hence, block scope).. PS: Thanks for all the help!]
I just leant that the data stored by fgets doesn't stay the same once the compiler is done executing the function in which fgets is being invoked.
What's the reason behind this? Does the fgets' return data have a lifespan or something? If so, how did they even code a function such as fgets? Something that can keep track of the user's invocation scope?
Here's the code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 10
void input_string (char** pptr_one, char** pptr_two)
{
char buffer[SIZE];
printf("\nInput: ");
char* x = fgets(buffer, SIZE, stdin);
//Copying the original fgets result address:
*pptr_one = x;
//Copying its contents to another variable:
*pptr_two = malloc(SIZE);
strcpy(*pptr_two, x);
}
int main (int argc, char **argv)
{
char* ptr_one;
char* ptr_two;
input_string(&ptr_one, &ptr_two);
printf("\nptr_one is pointing to: <%s>\n", ptr_one);
printf("ptr_two is pointing to: <%s>\n", ptr_two);
return 0;
}