tags:

views:

671

answers:

6

I would like to know if a pointer stores the address of any 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?

Please help, I'm gettin confused ... :((

+4  A: 

Look at this SO post for a better understanding of pointers. http://stackoverflow.com/questions/5727/what-are-the-barriers-to-understanding-pointers-and-what-can-be-done-to-overcome

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.

Suvesh Pratapa
+9  A: 

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.

jeffamaphone
A: 

I think you need to clear your basic understanding of what a pointer is. Check this pointers in c

Naveen
+3  A: 

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
+17  A: 
Robert Cartaino
A: 

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.)

UncleO