Thread: Accessing structs in functions

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    1

    Accessing structs in functions

    Im having some issues while referring to values passed to a function from a pointer.

    Code:
    typedef struct {
     double x;
     double y;
    } Point;
    
    
    typedef struct {
     Point angle0;
     Point angle1;
     Point angle2;
    } Triangle;
    
    int main() {
        Triangle T;
        T.angle0 = enterPoint();
        T.angle1 = enterPoint();
        T.angle2 = enterPoint();
        printVertices(&T);
        printf ("The perimeter is %.2f", getPerimeter(&T));
    }
    
    void printVertices(const Triangle *tr) {
        printf ("The point coordinates are: ");
        printf ("[%.2f, %.2f], ", tr->angle0.x, tr->angle0.y);
        printf ("[%.2f, %.2f], ", tr->angle1.x, tr->angle1.y);
        printf ("and [%.2f, %.2f]\n", tr->angle2.x, tr->angle2.y);
    }
    
    
    double distanceBetweenPoints(const Point *p1, const Point *p2) {
        double x = (p2->x - p1->x) * (p2->x - p1->x);
        double y = (p2->x - p1->y) * (p2->x - p1->y);
        double distance = sqrt(x+y);
        return distance;
    }
    
    
    double getPerimeter(const Triangle* tr) {
        double a, b, c;
        a = distanceBetweenPoints((*tr).angle0, (*tr).angle1);
        b = distanceBetweenPoints(&tr.angle1, &tr.angle2);
        c = distanceBetweenPoints(&tr.angle2, &tr.angle0);
        double perimeter = a + b + c;
        return perimeter;
    }
    My issue lies in getPerimeter(). I have tried multiple solutions of grabbing the values stored in the 'tr' variable but I've had many errors. Incompatible types, request for member in something not a structure or union, and too few argument errors. Please assist me in calling the values of tr.angle0 , tr.angle1, and tr.angle2 in the getPerimeter() function

    nvm. Solved with distanceBetweenPoints(&tr->angle0, &tr->angle1);
    Last edited by thomal; 03-21-2019 at 08:22 PM. Reason: Solved

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Good to hear that you've solved it yourself. Have you considered naming those members points rather than angles, since the type is Point after all? I suspect readers would expect an angle to be a quantity in radians or degrees, rather than a Point.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing Structs in Functions Problem
    By opeth in forum C Programming
    Replies: 8
    Last Post: 04-02-2012, 09:11 PM
  2. Replies: 1
    Last Post: 05-30-2011, 12:10 PM
  3. accessing variables using structs
    By bazzano in forum C Programming
    Replies: 5
    Last Post: 04-25-2006, 11:09 AM
  4. accessing members in nested structs
    By amidstTheAshes in forum C Programming
    Replies: 2
    Last Post: 03-23-2005, 02:00 PM
  5. accessing array of structs values
    By WaterNut in forum C++ Programming
    Replies: 12
    Last Post: 07-08-2004, 08:47 PM

Tags for this Thread