8

If a pointer stores the address of a variable ... then from where do we get the pointer?

What I asked was that if we are using pointer directly, then there must be a location from where we get this pointer?

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • Retagged post. Ruby? Come on. – SuPra Jun 13 '09 at 17:17
  • 1
    As it stands, you question hardly makes sense. – Sinan Ünür Jun 13 '09 at 17:18
  • I pretty much answered your question, and a couple others showed you examples. Is there something else you need? – SuPra Jun 13 '09 at 17:29
  • 1
    You get the pointer from an API or another variable. If you don't initialize or assign a value to a particular pointer, it can point anywhere. In other words, your intuition is correct: the pointer value has to come from somewhere. – MSN Jun 19 '09 at 16:52
  • Replace API with function in my comment. – MSN Jun 19 '09 at 16:53

5 Answers5

30

Yes, a declared pointer has its own location in memory.

alt text

In the example above, you have a variable, 'b', which stores the value "17".

int b = 17;    /* the value of 'b' is stored at memory location 1462 */

When you create a pointer to that variable, the pointer is stored in its own memory location.

int *a;
a = &b;       /* the pointer 'a' is stored at memory location 874 */

It is the compiler's job to know where to "get the pointer." When your source code refers to the pointer 'a', the compiler translates it into -> "whatever address value is stored in memory location 874".

Note: This diagram isn't technically correct since, in 32-bit systems, both pointers and int's use four bytes each.

Community
  • 1
  • 1
Robert Cartaino
  • 27,494
  • 6
  • 45
  • 67
  • What about pointers to NULL fields? Specifically in recursive abstract data types. -- I'm learning about linked lists. A linked list is effectively an infinite number of pointers (recursive nodes). Do those take up memory? Or do null values not take up memory? – Federico Jul 17 '14 at 23:50
12

Yes. Below I have an int and a pointer to an int and code to print out each one's memory address.

int a;
printf("address of a: %x", &a);

int* pA = &a;
printf("address of pA: %x", &pA);

Pointers, on 32bit systems, take up 4 bytes.

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
4

In C:

char *p = "Here I am";

p then stores the address where 'H' is stored. p is a variable. You can take a pointer to it:

char **pp = &p;

pp now stores the address of p. If you wanted to get the address of pp that would be &pp etc etc.

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
3

Look at this SO post for a better understanding of pointers. What are the barriers to understanding pointers and what can be done to overcome them?

As far as your question goes, if I understand what you want, then, basically, when you declare a pointer, you specify an address or a numeric index that is assigned to each unit of memory in the system (typically a byte or a word). The system then provides an operation to retrieve the value stored in the memory at that address.

Community
  • 1
  • 1
SuPra
  • 8,488
  • 4
  • 37
  • 30
2

The compiler deals with translating the variables in our code into memory locations used in machine instructions.

The location of a pointer variable depends on where it is declared in the code, but programmers usually don't have to deal with that directly.

A variable declared inside a function lives on the stack or in a register, (unless it is declared static).

A variable declared at the top level lives in a section of memory at the top of the program.

A variable declared as part of a dynamically allocated struct or array lives on the heap.

The & operator returns the memory location of the variable, but unlike the * operator, it can't be repeated.

For example, ***i gets the value at the address **i, which is the value at address *i, which is the value stored in i, which the compiler figures out how to find.

But &&i won't compile. &i is a number, which is the memory location the compiler uses for the variable i. This number is not stored anywhere, so &&i makes no sense.

(Note that if &i is used in the source code, then the compiler can't store i in a register.)

fde-capu
  • 205
  • 1
  • 4
  • 14
UncleO
  • 8,299
  • 21
  • 29