does C allow this ?

P

pereges

I have a structure for 'vector'

typedef struct vector_struct
{
double x, y, z;
}vector;

I have a function defined for vectors called "vector_cross" which
basically takes cross product of two vectors

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);
}

Now I created a new structure for 'vertex' which is essentially the
same as vector(with x, y, z coordinates).

typedef vector vertex;

Now, will C allow me to use any data of type vertex with the
vector_cross function and other vector functions that I may have
defined ?
 
W

WANG Cong

pereges said:
I have a structure for 'vector'

typedef struct vector_struct
{
double x, y, z;
}vector;

I have a function defined for vectors called "vector_cross" which
basically takes cross product of two vectors

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);
}

Now I created a new structure for 'vertex' which is essentially the
same as vector(with x, y, z coordinates).

typedef vector vertex;

Now, will C allow me to use any data of type vertex with the
vector_cross function and other vector functions that I may have
defined ?

I think yes, since two types are compatible types.
 
C

Chris Dollin

pereges said:
I have a structure for 'vector'

typedef struct vector_struct
{
double x, y, z;
}vector;

I have a function defined for vectors called "vector_cross" which
basically takes cross product of two vectors

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);
}

Now I created a new structure for 'vertex' which is essentially the
same as vector(with x, y, z coordinates).

typedef vector vertex;

You haven't "created a new structure for 'vertex'". You've
introduced a new name (`vertex`) to denote the same type
as `vector` denotes, viz, `struct vector_struct`.
Now, will C allow me to use any data of type vertex with the
vector_cross function and other vector functions that I may have
defined ?

There's only one type here, so yes.

/Because/ there's only one type here, perhaps the new name for
it is misleading.

--
"The whole apparatus had the look of having been put /Jack of Eagles/
together with the most frantic haste a fanatically
careful technician could muster."

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England
 
C

christian.bau

I have a structure for 'vector'

typedef struct vector_struct
{
   double  x, y, z;

}vector;

I have a function defined for vectors called "vector_cross" which
basically takes cross product of two vectors

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);

}

Now I created a new structure for 'vertex' which is essentially the
same as vector(with x, y, z coordinates).

typedef vector vertex;

A typedef is just a different name for a type, so "struct
vector_struct", "vector" and "vertex" are exactly the same type. It
doesn't make any difference whatsoever whether which one of these
three you use.
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top