tags:

views:

1170

answers:

8

temp2, temp1 are pointers to some struct x:

struct FunkyStruct x;
struct FunkyStruct *temp1 = &x, *temp2 = &x;

Now, after execution of following lines:

temp2=temp1;
temp1=temp1->nxt;

...Will temp2 and temp1 still point to the same memory location? If not, please explain why they would be different.

+2  A: 

temp2 will not be updated, but temp1 will point to the next item. So if temp1 is 0x89abcdef and temp1->next is 0x89b00000, then after you're done, temp1 will be 0x89b00000 and temp2 will be 0x89abcdef.

Assuming you're making a linked list, of course.

terminus
+1  A: 

You're not really giving us enough information to answer your question. Are they starting out pointing to the same structure, or are they only both of type pointer to structure x? And if it's some struct x, what's the definition of the nxt field?

John Fiala
A: 

Different.

You've saved the address of what temp1 is initially pointed to into temp2. You then changed what temp1 is pointed to, not the variable at the other end of what temp1 is pointed to.

If you had done

temp2 = temp1;
*temp1 = temp1->foo;

then temp1 & temp2 will both be pointing to (the same) modified variable.

KTC
+1  A: 

No, assuming there are pointers like in C. temp2 would be pointing at the location of x and temp1 would be pointing at whatever the nxt pointer points to. Usually this would be the layout for a singly linked list.

itsmatt
+10  A: 

Initially, temp1 and temp2 both contain the memory address of x.

temp2 = temp1 means "assign the value of temp1 to temp2". Since they have the same value to start with, this command does nothing.

The expression temp1->next means "Look inside the data structure that temp1 points to, and return the value of the field next." So temp1 = temp1->next assigns the value of temp1->next to temp1. (Of course, the lookup happen before the assignment.) temp1 will now contain whatever value the next field happened to contain. It could be the same as the old value, or it could be different.

dysfunctor
and temp2 will have a different value( oh for the power to use the wiki functionality )
itj
+6  A: 

This sounds like a question based on a background in java?

The answer that dysfunctor gave is good.

The important thing to realise is that in C assigning a pointer is no different to assigning an integer.

Consider the following modification to your original code:

 int temp1 = 1;
 int temp2;
 temp2=temp1;
 temp1=temp1 + 1;

At the end of this temp1 is 2, temp2 is 1.

It's not like assigning a (non-primitive) object in java, where the assignment actually assigns a reference to the object rather than the value.

Andrew Edgecombe
A: 

x (and therefore x.nxt) will be initialised to an unspecified value, depending on the combination of compiler, compiler options and the runtime environment. temp1 and temp2 will both point to x (before and after temp1=temp2). Then temp1 will be assigned whatever value x.nxt has.

Final answer: 0 < Pr(temp1 == temp2) << 1, because temp1 == temp2 iff x.nxt == &x.

Marcelo Cantos
A: 

The short answer is no. But only if nxt is different to both temp1 and temp2 to start with.

The line temp1=temp1->nxt; has two parts, separated by the = operator. These are:

  • The right hand side temp1->nxt looks up the structure pointed to by temp1 and takes the value of the nxt variable. This is a pointer (new memory location).
  • The pointer from the right hand side is then used to update the value of temp1.
Thomas Bratt