Thread: help with pass by reference with structs.

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    7

    help with pass by reference with structs.

    good afternoon,

    i'm having some trouble with pass by reference using structs. So far I think i'm on somewhat the right track but the portion that's giving me trouble at the moment is the PrintVector() function. I've removed a lot of the items to see if a blank slate would help me solve this but I have yet to wrap my head around this problem. i'm not sure where to begin with it and I've spent the better part of my morning on this one section my main problem is how to get the other two functions to pass their values onto the PrintVector() function as every time I've tried this I can't seem to get it right. My main error seems to be that I have too few arguments to call the function in a pass by reference, but I don't see where all the arguments come into play in the PrintVector() function. any nudge in the right direction would be super appreciated. thank you

    Code:
    #include<stdio.h>#include<stdlib.h>
    #include<math.h>
    
    
    typedef struct 
    {
    	double x;
    	double y;
    	double z;
    } Vector;
    
    
    void AddVector(const Vector *Pv1, Vector *Pv2)
    {
    	Vector newVectorAdd;
    
    
    	newVectorAdd.x =  Pv1-> x +  Pv2-> x;
    	newVectorAdd.y =  Pv1-> y +  Pv2-> y;
    	newVectorAdd.z =  Pv1-> z +  Pv1-> z;
    }
    
    
    void SubtractVector(const Vector *Pv1, Vector *Pv2)
    {
    	Vector newVectorSub;
    
    
    	newVectorSub.x = Pv1-> x - Pv2-> x;
    	newVectorSub.y = Pv1-> y - Pv2-> y;
    	newVectorSub.z = Pv1-> z - Pv1-> z;
    	
    }
    
    
    void PrintVector(const Vector *Pv1, Vector *Pv2)
    {
    	Vector ;
    
    
    	AddVector();
    	printf("Addition Result: (%.02lf, %.02lf, %.02lf)", add.x, add.y, add.z);
    
    
    	SubtractVector();
    	printf("Subtraction Result: (%.02lf, %.02lf, %.02lf)", sub.x, sub.y, sub.z);
    }
    
    
    void main()
    {
    	Vector v1, v2;
    
    
    	printf("Enter the first value of the first vector in meters (X): ");
    	scanf_s("%lf", &v1.x);
    
    
    	printf("Enter the second value of the first vector in meters (Y): ");
    	scanf_s("%lf", &v1.y);
    
    
    	printf("Enter the third value of the first vector in meters (Z): ");
    	scanf_s("%lf", &v1.z);
    
    
    	printf("Enter the first value of the second vector in meters (X): ");
    	scanf_s("%lf", &v2.x);
    
    
    	printf("Enter the second value of the second vector in meters (Y): ");
    	scanf_s("%lf", &v2.y);
    
    
    	printf("Enter the third value of the second vector in meters (Z): ");
    	scanf_s("%lf", &v2.z);
    
    
    	PrintVector(v1, v2);
    
    
    	system("pause");
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Look at this snippet:
    Code:
     
    void AddVector(const Vector *Pv1, Vector *Pv2)
    {
        Vector newVectorAdd;
     
     
        newVectorAdd.x =  Pv1-> x +  Pv2-> x;
        newVectorAdd.y =  Pv1-> y +  Pv2-> y;
        newVectorAdd.z =  Pv1-> z +  Pv1-> z;
    }
    Do you realize that newVectorAdd is local to this function?

    Do you realize that since you never change any of the parameters and you are returning nothing this function is actually doing nothing more than wasting time?


    Jim

  3. #3
    Registered User
    Join Date
    Jan 2017
    Posts
    7
    no i didn't realize that, thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-14-2012, 07:45 PM
  2. Pass by value/reference
    By Niels_M in forum C Programming
    Replies: 14
    Last Post: 11-07-2010, 01:35 PM
  3. Pass by reference vs pass by pointer
    By Non@pp in forum C++ Programming
    Replies: 10
    Last Post: 02-21-2006, 01:06 PM
  4. pass by reference .... HELP!!
    By NamelessNoob in forum C++ Programming
    Replies: 19
    Last Post: 02-15-2006, 12:50 PM
  5. pass be reference versus pass by value
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-01-2002, 01:03 PM

Tags for this Thread