0

I think that I have the basic concept of pointers pretty clear now (after considering it, reading and re-watching the course material a few times) but I still have a question.

A pointer is essentially a new type of variable which stores the location in memory of some data. The type of pointer also tells the compiler how to treat the data at that location. So that, for example, with regards to an int*, when dereferenced, the compiler will look at the first 4 bytes at that location and interpret the ones and zeroes as an int, whereas with regards to a char*, when dereferenced, the compiler will look at only the first byte at that location and interpret the ones and zeroes as a char.

But that leads me to my question. If so, what's the difference between an int* and an int (for example)? Meaning, let's say that my program creates and initializes an int (for example int x = 5;). It's not like the compiler treats x as if x were a macro and replaces x with 5 wherever x turns up. Instead there's probably a lookup table somewhere which stores the address of x and whenever the compiler sees x it looks up that address and dereferences it. If so, does that mean that every variable is really a pointer? Am I making a mistake?

If I am not, then the logical consequences are: (1) Every variable is in fact a pointer. (2) Whenever you refer to a variable in your code the compiler automatically deferences it.

0

Both are different data types, but here's the thing. There are two different times

  1. compile time.
  2. run time.

The compiler doesn't replace x anywhere in the program with the value associated with it unless x is a compile-time constant. Otherwise, the value of x might get changed at run time and the compiler has nothing to do with that.

I haven't really read about that before, but what I imagine the compiler is doing is sort of injecting the address of that variable somehow and at a very low level and when the program is being executed, this address is derferenced somehow and the value is used.

This doesn't mean that every variable is a pointer, but rather every pointer is a variable. In other words, a variable is a location in memory where you can store data.

A pointer to that variable is another variable that stores a value that forms the memory address of the original variable.

Hope that helps!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .