Hi
Could anybody fill the table for me.
thanks

Declarations and initializations
int i=3,j=5,*p=&i,*q=&j,*r;
double x;

value               Equivalent expression          value
p==&i; 
**&p 
r=&x 
7**p/*q+7 
3**q-*p

What excutly means:
**&p
and
*&p

Ihave seen them somewhere, but I don't know what it means

thanks

Dani AI

Generated

Short clarification and a few practical tips to clear the remaining confusion.

is right that p == &i is a boolean test (does p hold i's address?). correctly pointed out the canceling behavior of address-of and indirection operators; a couple of extra notes make that safer: & yields a pointer type, * yields an lvalue of the pointed-to type. When the types line up, applying them in opposite order is essentially an identity at the language level — but it can still be run-time undefined behavior if you dereference an invalid pointer. For example:

int n = 42;
int *pn = &n;
int copy = *&n;      // copy == 42
int *same = &*pn;    // same == pn   (undefined if pn is null)

Operator precedence is what makes the arithmetic-looking expressions parse the way you expect. Unary * binds before binary * and /, so 7 * (*p) / (*q) + 7 (with the pointers dereferenced) is evaluated as ((7 * (*p)) / (*q)) + 7. With the values given in the original declaration (i == 3, j == 5) that computes as integer arithmetic: 21/5 == 4, so the whole expression becomes 11. The other expression is 3 * (*q) - (*p) and evaluates to 12.

Two important cautions that fill gaps in the thread: the variable r declared with the int pointers in the first post cannot legally be assigned the address of a double in C++ — that is a type mismatch and will be rejected (or require an explicit, unsafe cast). Also, any pointer declared but not initialized contains an indeterminate value; dereferencing it is undefined behavior. For reference on precedence and casts see the C++ operator-precedence notes and the guidance on pointer casts and conversions: operator precedence and reinterpret_cast / pointer conversions.

Recommended Answers

All 4 Replies

>>p==&i;
That is a logical expression, not an assignment. Its asking if the address stored in pointer p is the same as the address of i.

yeep
I know also that r=&x is an assignment statement and r is equall the address of x
7**p/*q+7 could be (Im not sure, can somebody correct me if not ) - 7*(*p/*q)+7
3**q-*p could be 3*(*q)-*p
but what about
**&p
*&p

thanks

**&p == *(*&p) == *p
*&*&*&x == *&*&x == *&x == x

unary & is the addressof operator; unary * is the dereference operator. so

&x

== address of x (pointer to x)

*&x

is dereference of the pointer to x ie. dereference address of x (== x itself).

Narue has written an excellent (but somewhat lengthy) article about pointers

If you care to learn more about pointers its well worth the read


edit: Another good article, from DaWei, can be found here

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.