Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > why i am getting error ??

Reply
Thread Tools

why i am getting error ??

 
 
broli
Guest
Posts: n/a
 
      03-28-2008
/* 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 ??



 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      03-28-2008
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
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      03-28-2008
broli <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 4.1 Why am I getting long decimals (eg, 19.9499999999999) instead of the numbers I should be getting (eg, 19.95)? PerlFAQ Server Perl Misc 0 04-17-2011 04:00 AM
FAQ 4.1 Why am I getting long decimals (eg, 19.9499999999999) instead of the numbers I should be getting (eg, 19.95)? PerlFAQ Server Perl Misc 0 01-16-2011 05:00 PM
Getting error error: 26 - Error Locating Server/Instance Specified juvi ASP .Net 3 01-22-2009 12:05 PM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM



Advertisments