Thread: Errors in the following C program

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    31

    Errors in the following C program

    Struggling with these errors it seems that most of them are in the same area can't figure out why?

    Code:
    #include "area.h"#include<stdio.h>
    #include<math.h>
    
    
    
    
    /* this function calculates the area of a convex polygon */
    
    
    double area(node *list)
    {
    
    
    
    
     node * a;
     node * b;
     node * c = list->next->next;
     double area = (0.5*( a(b-c) + b(c - a)) + c(a - b)); /* formula used to calculate triangle areas*/
     double total=0;
    
    
     while(c!=a){
      area =(0.5*( a(b-c) + b(c - a)) + c(a - b));
      total+=area;
      b=c;
      c=c->next;
    
    
              }
    
    
       return area;
    
    
     }
    error
    Code:
    icc -ansi -Wall -c -o area.o area.c
    area.c(27): error: expression preceding parentheses of apparent call must have (pointer-to-) function type
       double area = (0.5*( a(b-c) + b(c - a)) + c(a - b)); /* formula used to calculate triangle areas, */
                            ^
    
    
    area.c(27): error: expression preceding parentheses of apparent call must have (pointer-to-) function type
       double area = (0.5*( a(b-c) + b(c - a)) + c(a - b)); /* formula used to calculate triangle areas, /
                                     ^
    
    
    area.c(27): error: expression preceding parentheses of apparent call must have (pointer-to-) function type
       double area = (0.5*( a(b-c) + b(c - a)) + c(a - b)); /* formula used to calculate triangle areas, */
                                                 ^
    
    
    area.c(31): error: expression preceding parentheses of apparent call must have (pointer-to-) function type
        area =(0.5*( a(b-c) + b(c - a)) + c(a - b));
                     ^
    
    
    area.c(31): error: expression preceding parentheses of apparent call must have (pointer-to-) function type
        area =(0.5*( a(b-c) + b(c - a)) + c(a - b));
                              ^
    
    
    area.c(31): error: expression preceding parentheses of apparent call must have (pointer-to-) function type
        area =(0.5*( a(b-c) + b(c - a)) + c(a - b));
                                          ^
    
    
    compilation aborted for area.c (code 2)
    Last edited by erald23; 12-02-2018 at 10:41 PM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    In math "b(" has a meaning that differs from C in C use "b*(" instead to have the same meaning as in math of times.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,644
    That's not the only problem. node is presumably a struct. a and b are uninitialized pointers to a node, and you are using them directly in an arithmetic calculation, which is certainly not what you want.

    Firstly, they need to point to an actual node, presumably from the list parameter. This is a total guess, but maybe something like
    Code:
    node *a = list;
    node *b = list->next;
    node *c = list->next->next;
     
    double total = 0.0;
     
    while (c != list) {
        double area = 0.5*(a->value * (b->value - c->value) + b->value * (c->value - a->value)) + c->value * (a->value - b->value);
        total += area;
        a = b;
        b = c;
        c = c->next;
    }
     
    return total;
    Don't just say "it doesn't work" since, as I said, this is a total guess just to give you the idea.
    (For instance, I have no idea if the value member is called "value".)
    Last edited by john.c; 12-03-2018 at 12:11 AM. Reason: forgot total+=area
    A little inaccuracy saves tons of explanation. - H.H. Munro

  4. #4
    Registered User
    Join Date
    Dec 2018
    Posts
    8
    while(c!=a) : you are comparing two pointers which store different addresses.
    area =(0.5*( a(b-c) + b(c - a)) + c(a - b)); : if you want to use the values, you have to use the asterisk-operator: *a, *b, *b
    c = c->next; : I would check if c is NULL after this line of code. you can do that like so directly in the loop-condition: while(c) etc.

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    1,644
    @ProgrammingJedi (not)

    while(c!=a) : you are comparing two pointers which store different addresses.
    That's an assumption. How do you know they always contain different addresses? Have you heard of a circular linked list?

    if you want to use the values, you have to use the asterisk-operator: *a, *b, *b
    Obviously wrong in this case. They are clearly pointing to a struct, so simply dereferencing them isn't going to give a meaningful value for arithmetic operations. He needs to use the -> operator and access whatever the data field(s) is/are.

    c = c->next; : I would check if c is NULL after this line of code.
    Why? Clearly the code is written for a circular list, where the last node points back to the first. Actually, the code is so badly written, it's impossible to tell what the real situation is, so you are assuming yet again.


    @erald23, Stop entitling your posts "error in program". Well, duh, obviously, that's why you're here. Put a little more effort into your questions. What are you trying to do? What does your node struct look like? Where does this "formula" come from? (post a link)
    A little inaccuracy saves tons of explanation. - H.H. Munro

  6. #6
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    105
    /* this function calculates the area of a convex polygon */
    What for a polygon: with 3, 5, 6, or more vertices? Regular or irregular?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Some errors in my Program
    By Steveqb14 in forum C Programming
    Replies: 2
    Last Post: 04-14-2014, 09:17 AM
  2. Errors in Dev-C++ program
    By sinking2008 in forum C Programming
    Replies: 5
    Last Post: 05-15-2008, 09:00 AM
  3. errors in c program
    By akshara.sinha in forum C Programming
    Replies: 1
    Last Post: 12-22-2007, 06:15 PM
  4. Errors in my program
    By gjack72 in forum C++ Programming
    Replies: 3
    Last Post: 11-17-2005, 02:45 PM
  5. Errors with program
    By nizbit in forum C Programming
    Replies: 37
    Last Post: 12-19-2004, 09:56 PM

Tags for this Thread