why i am getting error ??

Discussion in 'C Programming' started by broli, Mar 28, 2008.

  1. broli

    broli Guest

    /* vector.h */

    #ifndef VECTOR_H
    #define VECTOR_H

    #include<math.h>

    typedef struct vector_struct
    {
    double x, y, z;

    } vector;

    double vector_squared_length(vector* a) ;
    double vector_length(vector *a);
    vector *vector_negate(vector *a);
    double vector_dot(vector *a, vector *b);
    double vector_distance(vector *a, vector *b);
    vector *vector_normalize( vector *a);
    vector *vector_scale(vector *a, double newlength);
    vector *vector_add(vector *a, vector *b, vector *c);
    vector *vector_sub(vector *a, vector *b, vector *c);
    vector *vector_cross(vector *a, vector *b, vector *c);

    #endif

    /* vector.c */

    #include "vector.h"

    double vector_squared_length(vector* a)
    {
    return((a->x) * (a->x) + (a->y) * (a->y) + (a->z) * (a->z));
    }

    double vector_length(vector *a)
    {
    return (sqrt(vector_squared_length(a)));
    }

    vector *vector_negate(vector *a)
    {
    a->x = -a->x;
    a->y = -a->y;
    a->z = -a->z;

    return(a);
    }

    double vector_dot(vector *a, vector *b)
    {
    return((a->x)*(b->x) + (a->y)*(b->y) + (a->z) *(b->z));
    }

    double vector_distance(vector *a, vector *b)
    {
    double dx = a->x - b->x;
    double dy = a->y - b->y;
    double dz = a->z - b->z;

    return( sqrt(dx *dx + dy * dy + dz * dz) );
    }

    vector *vector_normalize( vector *a)
    {
    double len = vector_length(a);
    if( len!= 0.0 )
    {
    a->x /= len;
    a->y /= len;
    a->z /= len;
    }

    return(a);
    }

    vector *vector_scale( vector *a, double newlength )
    {
    double length = vector_length(a);

    if( length != 0.0 )
    {
    a->x *= newlength/length ;
    a->y *= newlength/length ;
    a->z *= newlength/length ;

    }

    return(a);
    }

    vector *vector_add( vector *a, vector *b, vector *c)
    {
    c->x = a->x + b->x ;
    c->y = a->y + b->y ;
    c->z = a->z + b->z;

    return(c);
    }

    vector *vector_sub( vector *a, vector *b, vector *c)
    {
    c->x = a->x - b->x;
    c->y = a->y - b->y;
    c->z = a->z - b->z;

    return(c);
    }

    vector *vector_cross( vector *a, vector *b, vector *c )
    {
    c->x = (a->y * b->z) - (a->z * b->y) ;
    c->y = (a->z * b->x) - (a->x * b->z) ;
    c->z = (a->x * b->y) - (a->y * b->x) ;

    return(c);
    }


    /* test.c */

    #include "vector.h"
    #include <stdio.h>

    void test_vector(void)
    {
    vector *a;
    vector *b;

    a = malloc(sizeof(vector));
    a->x = 3.455;
    a->y = 4.56;
    a->z = 6.77;


    printf(" vector squared length = %f\n", vector_squared_length(a));
    printf(" vector length = %f\n", vector_length(a));
    b = vector_negate(a);
    printf("negated vector is = %f\t%f\t%f\n",b->x, b->y,b->z);

    }

    int main(void)
    {
    test_vector();

    return 0;
    }


    vector.c compiles just fine but test.c is giving a warning -

    D:\PROJECT\test.c(9): warning #2027: Missing prototype for 'malloc'.

    and an error -

    D:\PROJECT\test.c(9): error #2168: Operands of = have incompatible
    types 'struct vector_struct *' and 'int'.

    Why is it wrong to use malloc like this ? The return type of malloc
    is void so will it not be typecasted into a vector pointer when
    assigned to one ??
     
    broli, Mar 28, 2008
    #1
    1. Advertising

  2. broli said:

    <snip>

    > a = malloc(sizeof(vector));


    <snip>

    > vector.c compiles just fine but test.c is giving a warning -
    >
    > D:\PROJECT\test.c(9): warning #2027: Missing prototype for 'malloc'.


    You need <stdlib.h>

    > Why is it wrong to use malloc like this ? The return type of malloc
    > is void so will it not be typecasted into a vector pointer when
    > assigned to one ??


    No, it will be implicitly converted from void * to vector * - but you do
    need to provide a function prototype for malloc, by including <stdlib.h>,
    because otherwise your compiler's "implicit int" rule cuts in, and the
    compiler gets horribly confused, and so do you.

    Headers, unlike parsley, are not just there for decoration.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Mar 28, 2008
    #2
    1. Advertising

  3. broli <> writes:
    <snip>
    > double vector_distance(vector *a, vector *b)
    > {
    > double dx = a->x - b->x;
    > double dy = a->y - b->y;
    > double dz = a->z - b->z;
    >
    > return( sqrt(dx *dx + dy * dy + dz * dz) );
    > }


    Since your question is answered, I'll just suggest that the above
    could have been:

    double vector_distance(vector *a, vector *b)
    {
    vector dv;
    vector_sub(a, b, &dv);
    return vector_length(&dv);
    }

    --
    Ben.
     
    Ben Bacarisse, Mar 28, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?VGltOjouLg==?=

    Why, why, why???

    =?Utf-8?B?VGltOjouLg==?=, Jan 27, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    603
    Juan T. Llibre
    Jan 27, 2005
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    990
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,255
    Smokey Grindel
    Dec 2, 2006
  4. juvi
    Replies:
    3
    Views:
    1,083
    Alexey Smirnov
    Jan 22, 2009
  5. George
    Replies:
    4
    Views:
    681
    Roedy Green
    Jun 28, 2012
Loading...

Share This Page