0

I'm taking CS50 via edx. One of the videos dealing with pointers (can't find it now) runs a program called noswap (under GDB) that highlights how C uses copies of variables (in this case, int a and int b) rather than the variables themselves (in this case, int x and int y).

Toward the end, the video runs "print &a" and receives something like "0xbfffefe4" as the memory location of "int a."

If "a" were a pointer to an int, how would one get the address to which "a" points as opposed to the address at which "a" resides?

Thanks in advance for any help.

2
  • Thanks very much for the comments. Commented Mar 8, 2015 at 22:07
  • Kareem--Your diagram illustrates very concisely and clearly exactly what pointer notation means. Commented Mar 8, 2015 at 22:10

3 Answers 3

0

If a is a variable of a pointer type, then &a returns a pointer to that variable (i.e., a pointer to a pointer) and *a returns the value that this variable points to (aka dereferences the pointer). Here's a visual representation of this relation:

++++     ++++     ++++
|  | ==> |  | ==> |  |
++++     ++++     ++++
 &a        a       *a
0

*a is how you deference a pointer, ie. go to it's address to see what it contains.

0

printf("%p", &*a);

returns the memory address of the location to which *a points.

You must log in to answer this question.

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