11 Replies - 364 Views - Last Post: 08 April 2013 - 06:40 AM Rate Topic: -----

#1 ZacCarlson  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,737
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 ZacCarlson  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#4 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 710
  • View blog
  • Posts: 2,033
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 ZacCarlson  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • 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!
Was This Post Helpful? 0
  • +
  • -

#6 Mrk  Icon User is offline

  • D.I.C Head

Reputation: 29
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#7 Mrk  Icon User is offline

  • D.I.C Head

Reputation: 29
  • View blog
  • 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......
Was This Post Helpful? 0
  • +
  • -

#8 ZacCarlson  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,737
  • 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.
Was This Post Helpful? 0
  • +
  • -

#10 ZacCarlson  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#11 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,737
  • 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.
Was This Post Helpful? 0
  • +
  • -

#12 Mrk  Icon User is offline

  • D.I.C Head

Reputation: 29
  • View blog
  • 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"?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1