comp.lang.c
[Top] [All Lists]

why i am getting error ??

Subject: why i am getting error ??
From: broli
Date: Fri, 28 Mar 2008 02:30:00 -0700 PDT
Newsgroups: comp.lang.c

/* 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 ??




<Prev in Thread] Current Thread [Next in Thread>
Privacy Policy