10 Replies - 495 Views - Last Post: 11 December 2012 - 02:27 PM Rate Topic: -----

#1 skyline2162  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 112
  • Joined: 17-December 11

I do not understand this output, question from past exam

Posted 11 December 2012 - 01:13 PM

I am requried to put the out put of the program given

class f2011fq1d
{
	unsafe public static void Main()
	{
		int a = 2;
		
		int b = 4;
		
		int* p;
		
		int* q;
		
		int[] ia = { 11, 12, 13 };
		
		p = &a;
		
		q = &b;
		
		Console.WriteLine(*p + a);
		Console.WriteLine(*q / *p);
		Console.WriteLine(*&a + *&b * 2);
		
		*p = a + *q;
		
		Console.WriteLine(a + *q);
		
		fixed (int* r = ia)
		{
			Console.WriteLine(*r + 3);
		}
	}
}



I get a = 2, b = 4, therefore p = 2 and q = 4.
First output i get its 4 (2+2)
Second output i get its 2 (4/2)
This output and on i do not get, i thought that the *&a dereferences the pointer and makes it the original, but that doesnt work.

The output of this program is 4
2
10
10
14

Can someone please explain how they got it, thank you!

Is This A Good Question/Topic? 0
  • +

Replies To: I do not understand this output, question from past exam

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,192
  • Joined: 02-June 10

Re: I do not understand this output, question from past exam

Posted 11 December 2012 - 01:31 PM

Rather than just telling you, I'd rather help you learn how to figure this stuff out yourself. You'll get a lot more out of a new skill than an answer.

My suggestion is you break down the complex formula into new varibles os you can actuall see and thus comprehend.

Breakdown 21 Console.WriteLine(*&a + *&b * 2);

into

int alpha = *&a;
int bravo = *&b;

Console.Writline(string.format("a: {0}, b: {1}", alpha, bravo);

Was This Post Helpful? 0
  • +
  • -

#3 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: I do not understand this output, question from past exam

Posted 11 December 2012 - 01:37 PM

What do you think the values of *&a, *&b and *r are?

Also, remember that the multiplicative operator; *, has a higher precedence than the additive operator; +. See here for the consequences of that in that 3rd output.

This post has been edited by CodingSup3rnatur@l-360: 11 December 2012 - 01:42 PM
Reason for edit:: Missed a word out

Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,192
  • Joined: 02-June 10

Re: I do not understand this output, question from past exam

Posted 11 December 2012 - 01:45 PM

Quote

dereferences the pointer and makes it the original


"And makes it the original"... What exactly do you mean by that? What do you think that means? Do you think it somehow undoes the last math operation or something?
Was This Post Helpful? 0
  • +
  • -

#5 skyline2162  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 112
  • Joined: 17-December 11

Re: I do not understand this output, question from past exam

Posted 11 December 2012 - 01:47 PM

okay starting with the Breakdown 21 Console.WriteLine(*&a + *&b * 2); you said,

we are outputting alpha and bravo, which are *&a and *&b, so its they referencing 2 and 4 respectivley correct

from my understanding when you have a * and & together, they remove the pointer and make the varable the original value
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,192
  • Joined: 02-June 10

Re: I do not understand this output, question from past exam

Posted 11 December 2012 - 01:54 PM

Console.WriteLine(*&a + *&b * 2)
they referencing 2 and 4 respectivley correct

*&a + *&b * 2
2 + 4 * 2
2 + 8
10

What's the problem?
Was This Post Helpful? 0
  • +
  • -

#7 skyline2162  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 112
  • Joined: 17-December 11

Re: I do not understand this output, question from past exam

Posted 11 December 2012 - 02:02 PM

sorry that was an error on my part, i didnt realize the order of operations there, if you don`t mind just helping me with this part

*p = a + *q;  
Console.WriteLine(a + *q);  

 fixed (int* r = ia)  
 {  

  Console.WriteLine(*r + 3);  

  }  




*p = a + *q, is this saying the original a which is 2 and *q is the reference of b which is 4, so 2+4=6, but again the answer is 10, i also don`t understand the fixed part, my knowledge of the fixed is that it is used for garbage collection, i do not get the code behind the int*r =ia
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,192
  • Joined: 02-June 10

Re: I do not understand this output, question from past exam

Posted 11 December 2012 - 02:10 PM

LIne 4: r becomes the address of the array. So the address of the first element of the array which is 11

Line 7: r + 3... 11 + 3... 14

Don't over think this stuff. Its 8th grade math. You're just making it harder than it really is.

This post has been edited by tlhIn`toq: 11 December 2012 - 02:10 PM

Was This Post Helpful? 0
  • +
  • -

#9 skyline2162  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 112
  • Joined: 17-December 11

Re: I do not understand this output, question from past exam

Posted 11 December 2012 - 02:15 PM

okay thanks, last question *p = a + *q, is this saying the original a which is 2 and *q is the reference of b which is 4, so 2+4=6, but answer is 10, where am i going wrong now
Was This Post Helpful? 0
  • +
  • -

#10 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,192
  • Joined: 02-June 10

Re: I do not understand this output, question from past exam

Posted 11 December 2012 - 02:23 PM

Stop with this "original" stuff. I have no idea what you are thinking this means.

Before the output on line 25 - what does line 23 do?
Was This Post Helpful? 0
  • +
  • -

#11 skyline2162  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 112
  • Joined: 17-December 11

Re: I do not understand this output, question from past exam

Posted 11 December 2012 - 02:27 PM

*p = a + *q; this makes p a new value which is the sum of a + *q, which is 2+4=6, that is how i see it
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1