why i am getting error ??

B

broli

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

Richard Heathfield

broli said:

a = malloc(sizeof(vector));

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 said:
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.
 
B

Ben Bacarisse

broli said:
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);
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top