passing structs in and out of functions

S

spleen

Hiya,

Im a newbie to C so please excuse my ignorance...

Ok, so I have several C files, with my functions in, one has main in
of course! and I have 2 structs as outside of the funtions (therefore
global?) in one of the files.

I can pass my information into the structs with no major difficulties,
that was easy! anyway now im trying to read from the structure into a
fuction so i can use the variables. this is where my difficulties
start and I believe its due to either my prototype or a pointer?
although im really not sure hence the question...

so this is my struture which is stored in file1.c

typedef struct{
int TRI_1;
int TRI_2;
int TRI_3;
}Triangles;

and then in main.c I have a function like this

void render(Triangles Tri)
{
int a;
a = Tri[n].TRI_1;
}

which has a prototype at the top like this

void render(Triangles tria, Vectors Vecs)

I have cut down on what I have actually wrote so its easier to read,
but thats the basis of it. I dont understand structures enough to do
figure this out. Ive read various sources including but still dont get
it, can someone enlighten me?

also just out of curiousity, is this operater - -> called the infix?

Thanks in advance, I am most grateful

dave
 
B

Barry Schwarz

Hiya,

Im a newbie to C so please excuse my ignorance...

Ok, so I have several C files, with my functions in, one has main in
of course! and I have 2 structs as outside of the funtions (therefore
global?) in one of the files.

I can pass my information into the structs with no major difficulties,
that was easy! anyway now im trying to read from the structure into a
fuction so i can use the variables. this is where my difficulties
start and I believe its due to either my prototype or a pointer?
although im really not sure hence the question...

so this is my struture which is stored in file1.c

typedef struct{
int TRI_1;
int TRI_2;
int TRI_3;
}Triangles;

This is not a struct. This is the declaration of a "new" type. You
have not shown us the definition of an object that has this type.
and then in main.c I have a function like this

void render(Triangles Tri)
{
int a;
a = Tri[n].TRI_1;

Tri is a single instance of the struct. It is not a pointer or an
array. Therefore, you cannot apply the subscript operator ([n]) to
it.
}

which has a prototype at the top like this

void render(Triangles tria, Vectors Vecs)

Make up your mind. Does render take one or two arguments.
I have cut down on what I have actually wrote so its easier to read,
but thats the basis of it. I dont understand structures enough to do
figure this out. Ive read various sources including but still dont get
it, can someone enlighten me?

also just out of curiousity, is this operater - -> called the infix?

It is called the pointer to structure dereference (or indirection)
operator.
Thanks in advance, I am most grateful

If you have a global struct defined in one source file, then you need
two things to be able to reference that struct in a second source
file:

You need the declaration of the struct type in the second file.
The usual method of accomplishing this is to place the declaration in
a header file and #include the header in both source files.

You need to declare the object in the second file with the
external storage class attribute.

Something like

header.h

typedef .... T;


main.c
#include "header.h"
T t; /*define object*/


second.c
#include "header.h"
extern T t; /*references to this object will resolve to the
one in main.c*/



<<Remove the del for email>>
 
N

Neil Kurzman

spleen said:
Hiya,

Im a newbie to C so please excuse my ignorance...

Ok, so I have several C files, with my functions in, one has main in
of course! and I have 2 structs as outside of the funtions (therefore
global?) in one of the files.

I can pass my information into the structs with no major difficulties,
that was easy! anyway now im trying to read from the structure into a
fuction so i can use the variables. this is where my difficulties
start and I believe its due to either my prototype or a pointer?
although im really not sure hence the question...

so this is my struture which is stored in file1.c

typedef struct{
int TRI_1;
int TRI_2;
int TRI_3;
}Triangles;

and then in main.c I have a function like this

void render(Triangles Tri)
{
int a;
a = Tri[n].TRI_1;
}

which has a prototype at the top like this

void render(Triangles tria, Vectors Vecs)

I have cut down on what I have actually wrote so its easier to read,
but thats the basis of it. I dont understand structures enough to do
figure this out. Ive read various sources including but still dont get
it, can someone enlighten me?

also just out of curiousity, is this operater - -> called the infix?

Thanks in advance, I am most grateful

dave

I hope I have this right but.... Try

void render(*Triangles tria); // the Prototype

int main(void)
{
Triangles aTri; // the variables

render(&aTri); // the call
}

//the function
void render(*Triangles Tri)
{
int a;
a = Tri->TRI_1;
Tri->TRI_2 = 75;
Tri->TRI_3 = Tri->TRI_3/2;
}
// note you could also use a = (Tri.)TRI_1;
// the parentheses maybe wrong, but the point is you need to use the
parentheses to deal with the higher precedence of the Dot operator.
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top