Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > why i am gettin...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 3 Topic 25162 of 27343
Post > Topic >>

why i am getting error ??

by broli <Broli00@[EMAIL PROTECTED] > Mar 28, 2008 at 02:30 AM

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




 3 Posts in Topic:
why i am getting error ??
broli <Broli00@[EMAIL   2008-03-28 02:30:00 
Re: why i am getting error ??
Richard Heathfield <rj  2008-03-28 09:42:08 
Re: why i am getting error ??
Ben Bacarisse <ben.use  2008-03-28 12:40:49 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Sep 6 21:24:40 CDT 2008.