Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm not entirely sure what you mean by "numeric pointer" as opposed to "char pointer". In C, a <code>char</code> is an integer type, so it is an arithmetic type. In any case, initialization is not required for a pointer, regardless of whether or not it's a pointer to <code>char</code>. </p> <p>Your code has the mistake of using <code>*p_message</code> instead of <code>p_message</code> to set the value of the pointer: </p> <pre><code>*p_message = "Pointer" // Error! </code></pre> <p>This wrong because given that <code>p_message</code> is a pointer to <code>char</code>, <code>*p_message</code> should be a <code>char</code>, not an entire string. But as far as the need for initializing a <code>char</code> pointer when first declared, it's not a requirement. So this would be fine:</p> <pre><code>char *p_message; p_message = "Pointer"; </code></pre> <p>I'm guessing part of your confusion comes from the fact that this would not be legal:</p> <pre><code>char *p_message; *p_message = 'A'; </code></pre> <p>But then, that has nothing to do with whether or not the pointer was initialized correctly. Even as an initialization, this would fail:</p> <pre><code>char *p_message = 'A'; </code></pre> <p>It is wrong for the same reason that <code>int *a = 5;</code> is wrong. So why is that wrong? Why does this work:</p> <pre><code>char *p_message; p_message = "Pointer"; </code></pre> <p>but this fail?</p> <pre><code>char *p_message; *p_message = 'A'; </code></pre> <p>It's because there is no memory allocated for the <code>'A'</code>. When you have <code>p_message = "Pointer"</code>, you are assigning <code>p_message</code> the address of the first character <code>'P'</code> of the string literal <code>"Pointer"</code>. String literals live in a different memory segment, they are considered immutable, and the memory for them doesn't need to be specifically allocated on the stack or the heap. </p> <p>But <code>char</code>s, like <code>int</code>s, need to be allocated either on the stack or the heap. Either you need to declare a <code>char</code> variable so that there is memory on the stack:</p> <pre><code>char myChar; char *pChar; pChar = &amp;myChar; *pChar = 'A'; </code></pre> <p>Or you need to allocate memory dynamically on the heap:</p> <pre><code>char* pChar; pChar = malloc (1); // or pChar = malloc (sizeof (char)), but sizeof(char) is always 1 *pChar = 'A'; </code></pre> <p>So in one sense <code>char</code> pointers are different from <code>int</code> or <code>double</code> pointers, in that they can be used to point to string literals, for which you don't have to allocate memory on the stack (statically) or heap (dynamically). I think this might have been your actual question, having to do with memory allocation rather than initialization. </p> <p>If you are really asking about initialization and not memory allocation: A pointer variable is no different from any other variable with regard to initialization. Just as an uninitialized <code>int</code> variable will have some garbage value before it is initialized, a pointer too will have some garbage value before it is initialized. As you know, you can declare a variable:</p> <pre><code>double someVal; // no initialization, will contain garbage value </code></pre> <p>and later in the code have an assignment that sets its value:</p> <pre><code>someVal = 3.14; </code></pre> <p>Similarly, with a pointer variable, you can have something like this:</p> <pre><code>int ary [] = { 1, 2, 3, 4, 5 }; int *ptr; // no initialization, will contain garbage value ptr = ary; </code></pre> <p>Here, <code>ptr</code> is not initialized to anything, but is later assigned the address of the first element of the array. </p> <p>Some might say that it's always good to initialize pointers, at least to <code>NULL</code>, because you could inadvertently try to dereference the pointer before it gets assigned any actual (non-garbage) value, and dereferencing a garbage address might cause your program to crash, or worse, might corrupt memory. But that's not all that different from the caution to always initialize, say, <code>int</code> variables to zero when you declare them. If your code is mistakenly using a variable before setting its value as intended, I'm not sure it matters all that much whether that value is zero, <code>NULL</code>, or garbage. </p> <hr> <p><strong>Edit</strong>. OP asks in a comment: <em>You say that "String literals live in a different memory segment, they are considered immutable, and the memory for them doesn't need to be specifically allocated on the stack or the heap", so how does allocation occur?</em></p> <p>That's just how the language works. In C, a string literal is an element of the language. The C11 standard specifies in §6.4.5 that when the compiler translates the source code into machine language, it should transform any sequence of characters in double quotes to a static array of <code>char</code> (or <code>wchar_t</code> if they are wide characters) and append a <code>NUL</code> character as the last element of the array. This array is then considered immutable. The standard says: <code>If the program attempts to modify such an array, the behavior is undefined.</code> </p> <p>So basically, when you have a statement like:</p> <pre><code>char *p_message = "Pointer"; </code></pre> <p>the standard requires that the double-quoted sequence of characters <code>"Pointer"</code> be implemented as a static, immutable, <code>NUL</code>-terminated array of <code>char</code> somewhere in memory. Typically implementations place such string literals in a read-only area of memory such as the text block (along with program instructions). But this is not required. The exact way in which a given implementation handles memory allocation for this array / <code>NUL</code> terminated sequence of <code>char</code> / string literal is up to the particular compiler. However, because this array exists somewhere in memory, you can have a pointer to it, so the above statement does work legally. </p> <p>An analogy with function pointers might be useful. Just as the code for a function exists somewhere in memory as a sequence of instructions, and you can have a function pointer that points to that code, but you cannot change the function code itself, so also the string literal exists in memory as a sequence of <code>char</code> and you can have a <code>char</code> pointer that points to that string, but you cannot change the string literal itself.</p> <p>The C standard specifies this behavior only for string literals, not for character constants like <code>'A'</code> or integer constants like <code>5</code>. Setting aside memory to hold such constants / non-string literals is the programmer's responsibility. So when the compiler comes across statements like:</p> <pre><code>char *charPtr = 'A'; // illegal! int *intPtr = 5; // illegal! </code></pre> <p>the compiler does not know what to do with them. The programmer has not set aside such memory on the stack or the heap to hold those values. Unlike with string literals, the compiler is not going to set aside any memory for them either. So these statements are illegal. </p> <p>Hopefully this is clearer. If not, please comment again and I'll try to clarify some more. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload