0

suppose I have the following code in C:

int x = 1025;
int *y = &x;
  1. What is &y? Is it the address in memory of the pointer variable y?
  2. What is the pointer variable y? Does it hold the address of the variable x?
  3. What is &x? Is it the address in memory of the variable x itself or is it the address its value, 1025?
  4. What is the variable x? Is it the address of its value, 1025?
  5. What is *y? Is it the address of 1025 = variable x ?

and the big one...

If printing &y displays the address of y, printing &x displays the address of x, but printing x/*y just prints 1025, how would I print the address of 1025?

Sorry if some of these questions seem really obvious, but I've had a lot of confusion messing with pointers that I'm trying to clear up.

[EDIT] Guys, I found my mistake; I was thinking that the variable x would hold the address of the sequence of bytes representing 1025. Standard Java-to-C maladjustment. Thank you all for the responses

5
  • What did you try? Did you write some C code (e.g. compiled with gcc -Wall -g) and then step by step it using some debugger (e.g. gdb)? Show much more of your C code. Commented Sep 7, 2013 at 20:25
  • For you big question, numbers don't have addresses. Do you mean where 1025 is stored (in x)? Commented Sep 7, 2013 at 20:37
  • This is not how pointers work. I suggest you read a good C book. We are not here to teach you an entire language but to help with specific programming questions. Commented Sep 7, 2013 at 20:37
  • 2
    He's asking a question. "how would I print the address of 1025?" But first he's wisely laying out his assumptions, to let us know where his confusion is coming from. Commented Sep 7, 2013 at 20:51
  • @H2CO3...can you please tell me what is wrong with my answer??? Commented Sep 7, 2013 at 21:25

6 Answers 6

3

Given:

int x = 1025;
int *y = &x;

Q1. What is &y? Is it the address in memory of the pointer variable y?

Yes. It is the address of the variable y, and has the type int **.

Q2. What is the pointer variable y? Does it hold the address of the variable x?

Yes. It is a pointer variable that (after the code fragment) holds the address of x.

Q3. What is &x? Is it the address in memory of the variable x itself or is it the address its value, 1025?

It is the address of the variable x. It so happens that x holds 1025 at the moment, but when that value changes, &x still points to the variable x, and therefore to the current value of x and not to 1025 (because x contains a different value now).

Q4. What is the variable x? Is it the address of its value, 1025?

The variable x is a memory location that currently holds the value 1025, but this could change. Values such as 1025 do not themselves have addresses — that is a concept from OO languages (I think Smalltalk has that sort of concept; I think Java uses it for object types, but not the simple non-object types like int) and not a C concept.

Q5. What is *y? Is it the address of 1025 = variable x?

It is not the address of 1025; that's the easy part. If you use int i = *y;, then *y is the value in the variable that y points to, which is currently x, which currently has the value 1025, so i is set to 1025. If, however, you use *y = 1024;, then *y is the variable that y points to, which is x, so the value stored in x (and *y) changes from 1025 to 1024.

Q6. If printing &y displays the address of y, printing &x displays the address of x, but printing x/*y just prints 1025, how would I print the address of 1025?

The first two assertions are fine. The 'but' assertion needs to be written x / *y with a space between the / and the * as otherwise you have started a comment. However, that should not print 1025 but should print 1. If you are getting 1025, then you wrote something like:

printf("%d\n", x/*y);  /* comment */);

There is no address for 1025 per se. There might be various addresses for various variables that currently hold the value 1025, but there is no single address for the value 1025. That is a non-C concept, as already mentioned.

‡ If someone disagrees with the Java or Smalltalk references, let me know by comment and I'll remove the one those that are wrong. They are not a key part of my argument — I'm just trying to explain that some languages do have a concept loosely analogous to 'address of 1025', even though C does not.

Sign up to request clarification or add additional context in comments.

Comments

2

What is &y? Is it the address in memory of the pointer variable y?

Yep. You can have pointers-to-pointers as well.

int x = 357;
int *y = &x;
int **z = &y;

What is the pointer variable y? Does it hold the address of the variable x?

Yes. Basically, it is an integer-ish type that (as its value) holds the address of 'x'.

What is &x? Is it the address in memory of the variable x itself or is it the address its value, 1025?

The address to the variable, which, with an integer, is also where the value is stored.

What is the variable x? Is it the address of its value, 1025?

It is the location in memory of several bytes, that hold data. How the data is interpreted depends on the code that uses it. With integers, the data is meant to represent a number (like 1025) but it could just as easily be manipulated as if it was something else, like a couple chars or a float. Data in memory is merely data in memory - what gives it meaning is how it is used.

What is *y? Is it the address of 1025 = variable x ?

1025 is merely the data stored at the address of 'x', the value stored in those bytes of memory doesn't change the location of 'x' at all. Once 'x' is created, it's location in memory (for the duration of its lifetime) doesn't change.

So *y is x. *y 'dereferences' the address stored in 'y' (x's address), and so then you're operating on the block of memory that both 'x' and '*y' refer to.

If printing &y displays the address of y, printing &x displays the address of x, but printing x/*y just prints 1025, how would I print the address of 1025?

Printing x/*y should print 1. x = 1025. *y = 1025. 1025/1025 = 1 1025 doesn't have an address. 1025 is a bunch of bits in a few bytes somewhere. 1025 is the sequence of bits (that your code gives a meaning to, but that has no meaning on its own) stored in several bytes that is located at the address stored in 'x' and '*y'.

If we assume an integer is four bytes of memory (32 bit OS and hardware, let's say), then you have four bytes in some random location in RAM:

[01000100] [1101010] [00101001] [11010101] (gibbish binary for illustration)

The binary bits stored in those four bytes don't mean a thing, until your code decides how to interpret the meaning of those bits.

'x' is those four bytes. 1025 is converted to binary and stored in those four bytes. The address of 'x' is the address of those four bytes. 'y' is a chunk of bytes that you store the address of 'x' at. '*y' lets you operate on the bytes that 'y' stored the address of. Accessing *y gives the same bytes pointed to that x refers to.

Comments

1

What is &y? Is it the address in memory of the pointer variable y?

Yes.

What is the pointer variable y? Does it hold the address of the variable x?

Yes.

What is &x? Is it the address in memory of the variable x itself or is it the address its value, 1025?

It is the address of x. x can change, for example if you set it with 'x = 1024' in a later line, but its address would remain the same

What is the variable x? Is it the address of its value, 1025?

x is a variable, which means it takes up some space in memory, and the bits in that space have the integer value of 1025.

What is *y? Is it the address of 1025 = variable x ?

*y dereferences the pointer, which means it gets the value of the memory at that address. So it returns 1025.

Your 'big question' is just question #3.

Comments

1
  1. What is &y? Is it the address in memory of the pointer variable y?

    &y is the address at which the contents of variable y are stored, those contents being the address of (pointer to) an int.

  2. What is the pointer variable y? Does it hold the address of the variable x?

    Yes, y is a pointer that holds the address of variable x.

  3. What is &x? Is it the address in memory of the variable x itself or is it the address its value, 1025?

    &x is the address of x, so the address in memory of the variable itself, not of the constant value you initialized it with.

  4. What is the variable x? Is it the address of its value, 1025?

    The variable itself is just a region of storage in memory, an "object" in C terms. Internally, the compiler uses the name to find the address of that region of storage.

  5. What is *y? Is it the address of 1025 = variable x ?

    *y is the value of whatever y points to, in this case, 1025.

  6. how would I print the address of 1025?

    It is not legal to take the address of a literal constant in C (except string literals), for a variety of reasons, including that it might not have an address.

Comments

0

Yes, yes, the address (that's why & is called the "address of" operator), no, no.

Here's how you should think of variables in C. They are scraps of paper that you can write a value on. (The type of the value should match the type indicated on the piece of paper.) All the scraps of paper are bound together in a notebook so that you can address them by a page number, which we call the address.

Pointer variables are special pieces of paper on which you write the page number. You can later check the pointer, see which piece of paper it points to, and look at that instead.

It follows from this that:

  1. A variable has an address. It's a piece of paper with a page number.
  2. A value does not have an address. It's not a piece of paper, it's something like a number that you can write on a piece of paper. A number like 1025 does not have a page number, but when you write "1025" on a page you call x, then there is a page, with a page number, that has 1025 written on it.

To convince yourself that you can't take the address of a value, try something like &1025. The compiler won't allow that.

Comments

0
  1. exactly. A pointer is a variable and as a variable has its own memory address.

  2. Yes, the pointer y is initialized with the address of the variable x

  3. &x is the address of the variable x. You can print it if you want to clearly see the difference.

  4. The variable x is saved at a certain address in the memory. This is different from the value with which the variable is initialized, in this case 1025.

Again, to see the difference, try to print out the address of x and its value:

int x = 1025;
printf("%p", &x); //prints the address of x
printf("%d", x); //prints the value assigned to x

Last question: *y is the variable pointer and the dereference operator *, This means that you get the value saved in the variable that y points to.

Well actually if you print x/(*y) then you should get 1 as a result because you would have 1025/1025

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.