Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>I am wondering why this line of code is only printing the first character H of a literal character array string and not the full string "Hello"</p> </blockquote> <p>First, the pointer <code>char*</code> is defined as pointing to a character. In the case of your example, that is <code>H</code> in <code>Hello</code>. With a string, you are defining data in contiguous memory. For instance,</p> <pre><code>char [H][e][l][l][o][\0] index [0][1][2][3][4][5] </code></pre> <p>The pointer is used simply as a way of iterating over each character in memory. This means <code>myCharPointer = H(0)</code>, <code>++myCharPointer = e(1)</code>, etc.</p> <p>When you print the string, you would do the following:</p> <p><code>printf("The value of myCharPointer is %s \n \n", myCharPointer);</code></p> <blockquote> <p>I know that myIntArray is not defined as yet, but its just an example</p> </blockquote> <p>An integer pointer and an integer array are essentially the same thing in the context of assignment. A pointer can iterate through a block of memory and therefore your straight assignment <code>myIntArrayPointer = myIntArray</code> is correct.</p> <p><strong>As a side note:</strong></p> <p>If you have a regular character <code>char myChar = 'a';</code> you can get its address in memory with the <code>&amp;</code>. For instance, <code>char* myCharPointer = &amp;myChar;</code> -> <code>myCharPointer</code> now points to <code>myChar</code>. To get the value at the address, simply use the dereferencing operator <code>*myCharPointer</code> which would be <code>'a'</code>.</p>
 

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