Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>They're quite different beasts. To better explain, let me define both.</p> <p><strong>Pointers:</strong></p> <p>A variable holds some piece of data. A pointer is a type of data that refers to another piece of memory. Think of it as a sign that says "Over there ---->" pointing at an object of some sort. For example, strings in C are just a pointer to a character, and by convention, you know there's more characters following it until a <code>\0</code> character. C uses pointers extensively, since there's no other mechanism for sharing common information between parts of the program, except for....</p> <p><strong>Global Variables:</strong></p> <p>In a program, you have variables in each function. These can be the parameters to the function, and ones defined inside. As well, you have what are known as global variables. These variables store information that all the functions in a file can access. This can be useful to pass things like a global state around, or configuration. For example, you might have one called <code>debug</code> that your code checks before printing some messages, or to store a global state object, like the score in a video game.</p> <p><strong>What I think is confusing you:</strong> Both can be used to share information between parts of code. Because function arguments are passed <em>by value</em> in C, a function can't modify the variables of what calls it. There are two ways to "fix" that problem. The first (and correct) way is to pass a pointer to the variable into the function. That way, the function knows where to modify the parent's variable. Another approach is to just use a global variable. That way, instead of passing around pointers, they just edit the global variables directly.</p> <p>So you can use both of them to accomplish the same thing, but how they work is quite seperate. In fact, a global variable can be a pointer.</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. 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