Thread: Structures?

  1. #16
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by lilywest View Post
    Would you use all "->" instead of "." because when I do that I get:
    hw12.c:55: error: invalid type argument of '->'
    hw12.c:55: error: invalid type argument of '->'
    hw12.c:55: error: invalid type argument of '->'
    Clearly not, since you are getting all those errors.

    If you have a pointer to a struct, use -> to access a member
    If you have a plain struct, use . to access a member:

    Is random a pointer to struct? Yes, so use -> to get vertex.
    Is vertex[0] a pointer to struct? No, so use . to get x or y.

  2. #17
    Registered User
    Join Date
    Nov 2013
    Posts
    18
    update:
    Got rid of those errors, now code looks like:
    Code:
      printf ("The triangle:\n");
      printf ("\tVertex[0] is [%.02f, %.02f]\n", random.vertex[0].x, random.vertex[0].y);
      printf ("\tVertex[1] is [%.02f, %.02f]\n", random.vertex[1].x, random.vertex[1].y);
      printf ("\tVertex[2] is [%.02f, %.02f]\n", random.vertex[2].x, random.vertex[2].y);
      struct point centroid;
      printf ("\nThe centroid is: [%.02f, %.02f]\n", calcCentroid(centroid.x), i);
    }
    
    struct triangle calcCentroid (struct triangle *random)
    {
      struct point centroid;
      centroid.x = (random->vertex[0].x + random->vertex[1].x + random->vertex[2].x) / 3;
      centroid.y = (random->vertex[0].y + random->vertex[1].y + random->vertex[2].y) / 3;
      return;
    }
    and now getting:
    hw12.c: In function 'main':
    hw12.c:44: error: incompatible type for argument 1 of 'calcCentroid'


    as error.

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, calcCentroid requires a pointer-to-triangle. centroid.x is not a pointer-to-triangle, so that's not what you pass along.

  4. #19
    Registered User
    Join Date
    Nov 2013
    Posts
    18
    I don't think I understand what you're getting at... I changed the function header to:

    Code:
    struct point calcCentroid (struct triangle *random)
    and left everything else the same but still I'm getting the same error. I'm sorry I'm not getting it!

  5. #20
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well that's what you originally had, isn't it?

    Anyway, in order to calculate the Centroid of a triangle you need (unsurprisingly) a triangle, not a random point that you've picked from somewhere. You have to call this function with a triangle.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tree Structures Best Structures!!! Discuss.
    By MutantJohn in forum General Discussions
    Replies: 20
    Last Post: 09-07-2013, 01:17 AM
  2. Problems with Nested Structures and Arrays of Structures
    By Ignoramus in forum C Programming
    Replies: 4
    Last Post: 03-02-2010, 01:24 AM
  3. Accessing Structures Inside Structures
    By Mellowz in forum C Programming
    Replies: 1
    Last Post: 01-13-2008, 03:55 AM
  4. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  5. Replies: 5
    Last Post: 04-11-2002, 11:29 AM

Tags for this Thread