# Exam question help

Show 80 post(s) from this thread on one page
Page 1 of 8 12345678 Last
• 04-28-2010
ÉireKarl
Exam question help
Ok so I got this question in one of our exams and I havn't a clue on how to answer it. Can someone please explain what the question is asking?
Thanks :)

b) If a program contains the main function
Code:

```void main()           {  int X=2, Y=11;               func(X,&Y);               printf("%d %d\n",X,Y);           }```
and another function
Code:

```void func(int P, int *Q)             { int X;               X=(*Q);               P=X/3;             (*Q)=P+1;             }```
what two values will the main function print out?
• 04-28-2010
MK27
The question is asking what the output would be if you ran this program. It is mostly a basic arithmetic question.
• 04-28-2010
Terrak
have a read through it and work out what each line is doing to break it down into easier to understand points.
you may find you are over-complicating it, read the question and the functions carefully.
• 04-28-2010
claudiu
Hint, all of this:

Code:

```               X=(*Q);               P=X/3;             (*Q)=P+1;```
is just meant to confuse you.
• 04-28-2010
ÉireKarl
Ok thanks, but I'm still not really understanding how the first function has anything to do with the second. C does not click with me.
Is it saying Q is pointing to x from the first function?
Does X = 5/3?
And what does the
Code:

`func(X,&Y);`
mean?
• 04-28-2010
Terrak
pointers are very important and if you don't fully understand them then now is the best time to sit down and make sure you do.
have a look at;
Cprogramming.com Tutorial: Pointers
this explains about pointers and how they operate, after that see if it gives you any more insight.
if that doesn't help then just shout for help and im sure someone will try to expand on it.
use it to refer to your example from the question to see how it relates to it
• 04-28-2010
ÉireKarl
Quote:

Originally Posted by Terrak
pointers are very important and if you don't fully understand them then now is the best time to sit down and make sure you do.
have a look at;
Cprogramming.com Tutorial: Pointers
this explains about pointers and how they operate, after that see if it gives you any more insight.
if that doesn't help then just shout for help and im sure someone will try to expand on it.
use it to refer to your example from the question to see how it relates to it

Yeah I'm gonna give that tutorial another look over.
• 04-28-2010
ÉireKarl
So, anyone got an answer for me? :)
• 04-28-2010
quzah
Work through it on paper. What part is confusing to you?

Quzah.
• 04-28-2010
Overworked_PhD
Oh oh. I know. I know.
• 04-29-2010
ÉireKarl
Quote:

Originally Posted by quzah
Work through it on paper. What part is confusing to you?

Quzah.

Mainly the func(....) part. I just don't understand what it does.
• 04-29-2010
Overworked_PhD
Quote:

Originally Posted by ÉireKarl
Mainly the func(....) part. I just don't understand what it does.

Well gosh, that is like saying reality confuses Quzah. We don't know exactly which part gives him the problem(s).
• 04-29-2010
ÉireKarl
Quote:

Originally Posted by Overworked_PhD
Well gosh, that is like saying reality confuses Quzah. We don't know exactly which part gives him the problem(s).

Fine then you want me to code it :p?

Code:

`func(X,&Y);`
• 04-29-2010
You know that P and Q are just copies of X and Y, right?

so in func(), why don't you print up the values of P and Q and X and whatever else interests you.

See what these variables are all doing.

You know that since *Q is the address of Y, that whatever you do to it, will be affecting Y, but that's not true with X since the func() is not getting the address for it.

If a function is to change the value of a variable, it has to know the address of that variable.

You see that in simple things like scanf("%d", &number). Since scanf() is going to change the value of number, you have to give it the address of number.

But: printf("%d", number) - no address needed, since printf() is not going to change the value of the variable number.
• 04-29-2010
ÉireKarl
Quote:

Originally Posted by Adak
You know that P and Q are just copies of X and Y, right?

so in func(), why don't you print up the values of P and Q and X and whatever else interests you.

See what these variables are all doing.

You know that since *Q is the address of Y, that whatever you do to it, will be affecting Y, but that's not true with X since the func() is not getting the address for it.

If a function is to change the value of a variable, it has to know the address of that variable.

You see that in simple things like scanf("%d", &number). Since scanf() is going to change the value of number, you have to give it the address of number.

But: printf("%d", number) - no address needed, since printf() is not going to change the value of the variable number.

Ahh, ok thanks for narrowing that down!

So... does X=11 and Y=14/3 d(*.*)b?
Show 80 post(s) from this thread on one page
Page 1 of 8 12345678 Last