c++ - C pointer address printing

ID : 274539

viewed : 25

Tags : c++cc++c





Top 5 Answer for c++ - C pointer address printing

vote vote

91

Yes. All of your statements are correct. However in case of first

int *ip; 

it is better to say that ip is a pointer to an int type.

What happens if I print the result of ip?

It will print the address of x.

Will it print the address of variable x, something like

011001110   

No. Addresses are generally represented in hexadecimal. You should use %p specifier to print the address.

printf("Address of x is %p\n", (void *)ip);   

NOTE:
Note that in the above declaration * is not the indirection operator. Instead it specify the type of p, telling the compiler that p is a pointer to int. The * symbol performs indirection only when it appears in a statement.

vote vote

89

int x = 1, y = 2;  int *ip; // declares ip as a pointer to an int (holds an address of an int)  ip = &x; // ip now holds the address of x  y = *ip; // y now equals the value held at the address in ip 
vote vote

77

Consider the following as an example:

 Initializer       x        y        ip Memory Value      [1]      [2]      [1000] Memory Address    1000     1004     1008 

As you can see:

  1. x has the value 1 and the address 1000
  2. y has the value 2 and the address 1004
  3. ip has the value 1000 (the address of x) and the address 1008

Consider the following:

  1. x == 1 and &x == 1000
  2. y == 2 and &y == 1004
  3. ip == 1000 and &ip == 1008 and *ip == 1 (the value of x)

Hope this helps you visualize what's happening.

vote vote

70

It's all correct.

1st line: you declare two variables

2nd line: a memory pointer "ip" is defined

3rd line: The memory adress of X is given to the pointer ip

4th line: Y is now set to the value of the variable X at the address ip

The memory address, however, is in hexadecimal format. 011001110 is a byte of data and not a memory address. The address is more likely going to be something like 0x000000000022FE38 (it may be shorter). Consider this:

int x = 0; int *ptr = &x; //the "&" character means "the address of" printf("The variable X is at 0x%p and has the value of %i", (void *)ptr, x); 

This would print the address of X, rather than *ptr. You'd need another pointer to print the address of *ptr. But that's rather pointless, as a pointer is defined to print the address of a variable. Think of it as an alias for another variable (it's value is the value at that memory address).

vote vote

54

I like the original poster's statement that *ip is an int. This accurately reflects the semantics of declarations, where the declarator *ip is an expression and the declaration specifier(s), in this case int, provide the type of the expression. Stating the meaning this way provides insight into the meaning of something like int *ip, q; where q is simply an int. You might not want to use this more formal way of stating the type in conversation, but it is helpful in seeing the underlying structure expressed by the declaration.

Top 3 video Explaining c++ - C pointer address printing







Related QUESTION?