# Trying to pass x, y, z as parameters

Page 1 of 1

## 11 Replies - 1073 Views - Last Post: 08 April 2013 - 06:40 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=317910&amp;s=f8a937a07606ca521e0ffe16f5f4ee3e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ZacCarlson

Reputation: -7
• Posts: 146
• Joined: 08-October 12

# Trying to pass x, y, z as parameters

Posted 07 April 2013 - 04:02 PM

Error says expected primary expression before int on line 28. I don't understand and how to fix it.

```/* CS 261 - Assignment 1 - Q2
* Name: Zac Carlson
* Date: 04/07/2013
*/

#include <stdio.h>
#include <stdlib.h>

int foo(int *a, int *b, int c) {
/* Set a to double its original value */
*a = *a * *a;
/* Set b to half its original value */
*b = *b / 2;
/* Assign a+b to c */
c = *a + *b;
/* Return c */
return c;
}

int main() {
/* Declare three integers x, y, z and initialize them to 5, 6, 7 respectively */
int x = 5, y = 6, z = 7;
/* Print the values of x, y, z */
printf("X value: %d\t\n", x);
printf("Y value: %d\t\n", y);
printf("Z value: %d\t\n", z);
/* Call foo() appropriately, passing x, y, z as parameters */
foo(int *x, int *y, int z);
/* Print the value returned by foo */
printf("Value returned by foo: %d\t\n", foo(x, y, z));
/* Print the values of x, y, z again */
printf("X value: %d\t\n", x);
printf("Y value: %d\t\n", y);
printf("Z value: %d\t\n", z);
/* Is the return value different than the value of z?  Why? */

return 0;
}

```

Is This A Good Question/Topic? 0

## Replies To: Trying to pass x, y, z as parameters

### #2 CTphpnwb

• D.I.C Lover

Reputation: 3778
• Posts: 13,688
• Joined: 08-August 08

## Re: Trying to pass x, y, z as parameters

Posted 07 April 2013 - 04:16 PM

You can't call functions with variable declarations.

### #3 ZacCarlson

Reputation: -7
• Posts: 146
• Joined: 08-October 12

## Re: Trying to pass x, y, z as parameters

Posted 07 April 2013 - 04:21 PM

```foo(int *x, int *y, int z);
```

So you're saying to change it to:
```foo(*x, *y, z);
```
?

I did that before and it said invalid type argument of unary

### #4 snoopy11

• Engineering ● Software

Reputation: 1458
• Posts: 4,726
• Joined: 20-March 10

## Re: Trying to pass x, y, z as parameters

Posted 07 April 2013 - 04:26 PM

No i think he meant

foo(&X, &Y, &Z)

Regards

Snoopy.

### #5 ZacCarlson

Reputation: -7
• Posts: 146
• Joined: 08-October 12

## Re: Trying to pass x, y, z as parameters

Posted 07 April 2013 - 04:28 PM

I was just about to post that. I just did a little deeper thinking and figured it out. It is the &. Thanks snoop!

### #6 Mrk

Reputation: 29
• Posts: 66
• Joined: 03-December 09

## Re: Trying to pass x, y, z as parameters

Posted 07 April 2013 - 04:43 PM

On line 9, your function prototype indicates that the function accepts a pointer to an int for the first two parameters. Since the function accepts two pointers, where are the pointers that are to be passed to the function? If you recall, a pointer is just another variable that holds the address of another variable. for example, you may have a pointer called ptr_to_x that holds the memory address of x. You may also have another pointer called ptr_to_y that holds the address of y. Once you have those, THEN you can pass them to your function and your function can use them to manipulate the values of x and y from afar.

When you pass a pointer to a function, you don't have to do anything special. The compiler already knows that the pointer is a pointer because that is how it was declared when it was defined. And it is already understood that the function takes a pointer as an argument because the function prototype declares that. So just pass in the pointer to the function just like you would any other variable.

If you are using references, then it will be a little different.

### #7 Mrk

Reputation: 29
• Posts: 66
• Joined: 03-December 09

## Re: Trying to pass x, y, z as parameters

Posted 07 April 2013 - 04:52 PM

Are you required to use pointers for x and y, or references? References would be easier......

### #8 ZacCarlson

Reputation: -7
• Posts: 146
• Joined: 08-October 12

## Re: Trying to pass x, y, z as parameters

Posted 07 April 2013 - 04:55 PM

Line 12 prints the *a = .... two times, one with 5 and the other with 625. Why does it print it two times? How could I fix it. Isn't it just supposed to be 25? I did this on line 12 because my original code minus line 12 output:

Value returned by foo: 626
X value: 625
Y value: 1
Z value: 7

and I assume it's not right?

```/* CS 261 - Assignment 1 - Q2
* Name: Zac Carlson
* Date: 04/07/2013
*/

#include <stdio.h>
#include <stdlib.h>

int foo(int *a, int *b, int c) {
/* Set a to double its original value */
*a = *a * *a;
printf("*a = %d\t\n", a);
/* Set b to half its original value */
*b = *b / 2;
/* Assign a+b to c */
c = *a + *b;
/* Return c */
return c;
}

int main() {
/* Declare three integers x, y, z and initialize them to 5, 6, 7 respectively */
int x = 5, y = 6, z = 7;
/* Print the values of x, y, z */
printf("X value: %d\t\n", x);
printf("Y value: %d\t\n", y);
printf("Z value: %d\t\n", z);
/* Call foo() appropriately, passing x, y, z as parameters */
foo(&x, &y, z);
/* Print the value returned by foo */
printf("Value returned by foo: %d\t\n", foo(&x, &y, z));
/* Print the values of x, y, z again */
printf("X value: %d\t\n", x);
printf("Y value: %d\t\n", y);
printf("Z value: %d\t\n", z);
/* Is the return value different than the value of z?  Why? */

return 0;
}

```

We are to complete everything within the comments and that's it.
Also, it's double and I did double wrong. Duh. My bad. It's supposed to be 10.

This post has been edited by ZacCarlson: 07 April 2013 - 04:58 PM

### #9 CTphpnwb

• D.I.C Lover

Reputation: 3778
• Posts: 13,688
• Joined: 08-August 08

## Re: Trying to pass x, y, z as parameters

Posted 07 April 2013 - 04:58 PM

You're calling foo twice. Once in line 29 and again in line 31.

And A * A is not twice A. It's A squared.

### #10 ZacCarlson

Reputation: -7
• Posts: 146
• Joined: 08-October 12

## Re: Trying to pass x, y, z as parameters

Posted 07 April 2013 - 05:06 PM

Makes sense. However, in your opinion, if I took out line 29, would those two comments saying to call foo and to print foo satisfy them by just calling it in printf as I did?

### #11 CTphpnwb

• D.I.C Lover

Reputation: 3778
• Posts: 13,688
• Joined: 08-August 08

## Re: Trying to pass x, y, z as parameters

Posted 07 April 2013 - 05:22 PM

In my opinion you should use the fact that foo returns a value.

### #12 Mrk

Reputation: 29
• Posts: 66
• Joined: 03-December 09

## Re: Trying to pass x, y, z as parameters

Posted 08 April 2013 - 06:40 AM

When I get a letter returned from the post office, I keep on my desk. If someone asks me if I have the letter, I can say, "Yeah, right here" and hold up the letter. Since foo returns a value, where do you keep it? Can you point to it and say, "Yeah, it's right here"?