Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you're just about there, but your confusing the terminology. Let look at your examples again:</p> <blockquote> <p>andy = 25;<br> fred = andy;<br> ted = &andy;</p> </blockquote> <p>The first line assigns the value 25 to andy. The second line assigns the value held by andy to fred (25 in this case). The third line assigns the address of andy to ted.</p> <p>All three variables hold independent values. Changing any one of them will not affect the others.</p> <p>Now, looking at your next example:</p> <blockquote> <p>andy = 25;<br> fred = andy;<br> ted = &andy;<br> joe = *andy; </p> </blockquote> <p>Like you said, the first three lines are the same. The fourth line assigns the value pointed to by andy to joe. In this case joe would now hold the value at memory location 25 because andy is being treated as a memory location (and the actual value is unknown in this case because we don't know from the data given what is stored at address 25). </p> <p>And, again, all four variable hold independent values at this point. Changing any of them will not affect the value of the others.</p> <p>So lets look at another example:</p> <blockquote> <p>andy = 35;<br> jim = &andy; alex = *jim;</p> </blockquote> <p>The first line assigns the value of 25 to andy. Nothing new here. The second line assigns the address of andy to jim. The third line dereferences the address stored in jim and assigns the value to alex. The variable alex now holds the value 25. But the 25 that alex holds is a different 25 than what alan holds because alan and alex live at different memory addresses. If we change the value of alan to 30, alex still hold 25.</p> <p>A final example:</p> <blockquote> <p>andy = 25;<br> jim = &andy;<br> *jim = 30; </p> </blockquote> <p>The first two lines are just like the previous example, andy has a value of 25 and jim contains the address of andy. </p> <p>The third line assigns a value of 30 to the memory address pointed to by jim. Since this is the same address that andy lives at, andy now hold a value of 30. In this case andy and *jim reference the same memory location.</p> <p>The terms pass-by-value and pass-by-pointer (or pass-by-reference) refer to passing parameters to functions. When passing-by-value, as in:</p> <p><code>void myfunc(int a) { a = a+5; }</code></p> <p>And then called as in:</p> <blockquote> <p>int b=2;<br> myfunc(b);</p> </blockquote> <p>The value of b, upon return from the function is not changed because you merely passed the value of b into the function, assigning it to a. The variable a was changed but since it lives at a different memory address, changing a did not affect the value of b.</p> <p>But when passing-by-pointer, as in:</p> <p><code>void myfunc(int* a) { *a = *a+5; }</code></p> <p>And then called as in:</p> <blockquote> <p>int b=2;<br> myfunc(&amp;b);</p> </blockquote> <p>The value of b is changed from 2 to 7 because you passed the address of b to the function and then dereferenced the address to getit's current value, added 5 to it, and stored the result back in to the same address. When the function returns, the value stored at the address of b has been updated.</p>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    1. 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