views:

635

answers:

7

I'm reading The C Book to try and get a better foundation in C. While I think I'm generally getting the concept of pointers, one thing sticks out to me is that it seems like it's generalizing whatever it's pointing to into a global variable (e.g. the ability to use pointers to return values from void functions), which naturally carries with it all the attendant dangers, I assume.

Aside from the fact that a pointer references a specific variable or index in an array, what is the difference between a pointer and a global variable?

A: 

Completely different concepts. You can have pointers to both global and local variables. There's nothing associating the two.

Also, from a function, you can certainly return a pointer to a variable scoped within that function. But that's a bad idea since the variable existed on the function's stack and now that's gone.

xanadont
+7  A: 

A global variable is any variable that is accessible in any scope. A pointer is a variable that contains the address where something lives.

They aren't directly related to each other in any way.

A pointer variable can be in global or local scope and can also point to a variable that is in global, local, or no scope (as if it were coming off of the heap or addressing some DIO lines).

San Jacinto
+3  A: 

There's a huge difference. Aside from the "other" uses of pointers (which include dealing with strings and arrays, and building dynamic data structures like trees and linked lists), using a pointer to give another function access to a local variable is much more flexible and controlled than sharing a global variable between these two functions.

Firstly, it allows the called function to be provided access to different variables at different times. Think how much more laborious it would be to use scanf() if it always saved its results into the same global variables.

Secondly, passing a pointer to another function makes you much more aware of the fact that that function will be able to modify the object. If you use a global variable for the same purpose, it is easy to forget which functions modify the global and which do not.

Thirdly, global variables consume memory for the life of your program. Local variables are released when their containing function ends, and dynamically-allocated data is released when it is freed. So global variables can at times be a considerable waste of memory.

Using pointers leads to the danger of referring to variables that no longer exist, so care has to be taken. But this is most often a problem when there are complicated global or long-lived data structures which in itself is often a design weakness.

Globals just get in the way of good, modular program design and pointers often provide a better way to achieve the same things.

Artelius
+3  A: 

"Pointer" is a variable that tells you how to get to a value: it's the address of the value you care about. You dereference it (with *) to get to the value.

"Global" defines the scope of the variable: anywhere in the program can say the name and get the value.

You can have local pointers, or global non-pointers. The concepts are completely orthogonal.

Ken
+1  A: 
int x; /*Global variable 'x' accessible in any scope if not static */
/* x intialized to zero */
int main(void)
{
     int *ptr_to_x; /*pointer varible */
     ptr_to_x =&x;  /* pointer variable holding address of 'x' */

     /*More code*/
}

The above code clearly shows the difference between a global variable and a pointer variable.

Prasoon Saurav
+8  A: 

They're quite different beasts. To better explain, let me define both.

Pointers:

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 \0 character. C uses pointers extensively, since there's no other mechanism for sharing common information between parts of the program, except for....

Global Variables:

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 debug that your code checks before printing some messages, or to store a global state object, like the score in a video game.

What I think is confusing you: Both can be used to share information between parts of code. Because function arguments are passed by value 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.

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.

McPherrinM
A: 

The term pointer refers to a variable's type; it is a variable used to refer to another. The term global refers to a variables scope - i.e. its visibility from any part of a program. Therefore the question is somewhat nonsensical since they refer to different kinds of variable attribute; a pointer variable may in fact have global scope, and so have both attributes simultaneously.

While a pointer may indeed refer to an object that is not directly in scope (which is what I think you are referring to), it still allows restricted control of scope, because the pointer itself has scope (unless of course it is a global pointer!).

Moreover a global variable always has static storage class. Whereas a pointer may refer to a static, dynamic, or automatic variable, and because it is a variable, the pointer itself may be static, or auto, or in the case of a dynamically allocated array of pointers - dynamic also.

I think perhaps that you are considering only a very specific use of pointers when in fact they have far greater utility and can be used in many ways. For example, you would almost invariably use pointers to implement the links in a linked list data structure; a global variable will not help you do that.

Clifford

Clifford