SkyBlue posted:
Both of these variables are pointers. They are a non-const pointer to a
non-const char.
A pointer variable is just like any other variable -- it consumes memory
and stores data by arranging bits in a particular pattern.
The purpose of an int is to store an integer.
The purpose of a double is to store a floating-point number.
The purpose of a pointer is to store a memory address.
What is a memory address? 10? 5737? 2972529? I suppose we can presume it's
some sort of number, no?
On the particular system you're working with, a pointer variable might
consume 32 bits in memory; therefore, to store the memory address 2972529,
the pointer might store the following bit-pattern in memory:
00000000001011010101101101110001
Now, I'm going to shift the attention away from pointers for the moment,
and tell you a little about "automatic storage variables". When you define
a variable within a function, it's called an automatic storage variable
(unless of course you define it as "static" -- in which case it would be a
static duration variable.). If an automatic variable is not assigned a
value (as in your code snippet), then its value is indeterminant -- it
contains white noise, a bit-pattern left over from when that piece of
memory was last used. You have defined two automatic variables, but you
have not initialised them; therefore, they contain garbage, a random
number, a random memory address.
You then invoke "fgets" and provide it with these two random memory
addresses. In order to read or write memory, you must obliged to do so, you
must be permitted to do so. How do we gain permission to read and write a
certain block of memory... ? By allocating that memory for our own use, of
course. There's a function in the Standard Library called "malloc"; when
calling it, you specify how many bytes of memory you wish to allocate, and
the function returns the address of the newly allocated block of memory. So
let's store a valid address in our char pointers:
char *const one = malloc(512);
char *const two = malloc(512);
The values stored in "one" and "two" are not random -- they are the
addresses of dynamically allocated blocks of memory which are yours to
manipulate however you please.
Don't forget to then free the memory by passing the memory addresses to
"free":
free(one);
free(two);
I simplified a lot of the above (e.g. I haven't talked about trapping, or
the invocation of undefined behaviour by accessing an un-initialised
variable), but hopefully you get the idea.