Uninitialized pointer usage: one OK, other NOK?

This is a discussion on Uninitialized pointer usage: one OK, other NOK? within the C Programming forums, part of the General Programming Boards category; Hello to everyone! My first post, so be gentle.
Here I have two snippets: first should be avoided (unpredictable behavior), ...

Uninitialized pointer usage: one OK, other NOK?

Hello to everyone! My first post, so be gentle.

Here I have two snippets: first should be avoided (unpredictable behavior), second should be fine. I do not understand why is it so. (I have some knowledge of assembly (language) and computer architecture.)

In the first you are copying string ABCD to the memory location pointed to by k. However k is not pointing to a valid memory location because it is not initialized to point to one, nor is there enough space at that location to accept a string.

The second snippet pointer k points to a constant string. In this case it is not the string that is transferred to the memory location pointed to by k, but rather k made to point at the memory location where ABCD resides. That is fine, however k is 'read-only' in this case. You cannot change for example k[1].

Also when you post make sure to use CODE and /CODE tags between [] instead of PHP tags.

In the first you are copying string ABCD to the memory location pointed to by k. However k is not pointing to a valid memory location because it is not initialized to point to one, nor is there enough space at that location to accept a string.

Doesn't char *k; make k point to some "random" memory location? What does this statement really do behind the scenes? And how do you know that "there is not enough space"?

What is valid memory location, or for that matter, invalid? Is it, for example, memory range from [0] to [2^31-1] (for a 32-bit OS) and invalid is thus any location out of this range?

Originally Posted by claudiu

The second snippet pointer k points to a constant string. In this case it is not the string that is transferred to the memory location pointed to by k, but rather k made to point at the memory location where ABCD resides. That is fine, however k is 'read-only' in this case. You cannot change for example k[1].

Doesn't char *k; make k point to some "random" memory location? What does this statement really do behind the scenes? And how do you know that "there is not enough space"?

That's the point. k points to who-knows-where. There is a 99% chance that there isn't any allocated memory there. If there isn't, the application will crash.
But sometimes there can be. That's why it's called undefined behavior. The standard doesn't dictate what will happen.

What is valid memory location, or for that matter, invalid? Is it, for example, memory range from [0] to [2^31-1] (for a 32-bit OS) and invalid is thus any location out of this range?

A valid memory location is any location of allocated memory. We have about 2 GB to use in a 32-bit system, but remember that we're dealing with virtual memory. We could theoretically write anywhere in physical memory, but in virtual memory, the OS controls where we can and cannot write.
So wherever you have allocated memory is valid and everywhere else is not. That's basically it.

The string literal you try to assign is stored somewhere. In a read-only memory section of the file. So what the compiler does is take the address where that is stored and stores it inside the pointer.