Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There is a difference in the * usage when you are defining a variable and when you are using it.</p> <p>In declaration, </p> <pre><code>int *myVariable; </code></pre> <p>Means a pointer to an integer data type. In usage however,</p> <pre><code>*myVariable = 3; </code></pre> <p>Means dereference the pointer and make the structure it is pointing at equal to three, rather then make the pointer equal to the memory address 0x 0003.</p> <p>So in your function, you want to do this:</p> <pre><code>void makePointerEqualSomething(int* pInteger) { *pInteger = 7; } </code></pre> <p>In the function declaration, * means you are passing a pointer, but in its actual code body * means you are accessing what the pointer is pointing at.</p> <p>In an attempt to wave away any confusion you have, I'll briefly go into the ampersand (&amp;)</p> <p>&amp; means get the address of something, its exact location in the computers memory, so</p> <pre><code> int &amp; myVariable; </code></pre> <p>In a declaration means the address of an integer, or a pointer!</p> <p>This however</p> <pre><code>int someData; pInteger = &amp;someData; </code></pre> <p>Means make the pInteger pointer itself (remember, pointers are just memory addresses of what they point at) equal to the address of 'someData' - so now pInteger will point at some data, and can be used to access it when you deference it:</p> <pre><code>*pInteger += 9000; </code></pre> <p>Does this make sense to you? Is there anything else that you find confusing?</p> <p>@Edit3: </p> <p>Nearly correct, except for three statements </p> <pre><code>bar = *oof; </code></pre> <p>means that the bar pointer is equal to an integer, not what bar points at, which is invalid. </p> <p><code>&amp;bar = &amp;oof;</code> </p> <p>The ampersand is like a function, once it returns a memory address you cannot modify where it came from. Just like this code:</p> <pre><code>returnThisInt("72") = 86; </code></pre> <p>Is invalid, so is yours.</p> <p>Finally,</p> <pre><code>bar = oof </code></pre> <p>Does not mean that "bar points to the oof pointer." Rather, this means that bar points to the address that oof points to, so bar points to whatever foo is pointing at - not bar points to foo which points to oof.</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