C, structs and return values

So I created a struct here in my model.h file:
typedef struct
{
float* vertexBuffer;
float* Faces_Triangles;
float* normals;
long TotalConnectedTriangles;
} model;

under this 'model' struct, I have a model.c
I have this inserting my data into the struct, plus the return:

...
model* newModel = (model*)malloc( sizeof(model) );

newModel->vertexBuffer;
newModel->Faces_Triangles;
newModel->normals;
newModel->TotalConnectedTriangles;

return newModel;
}




So In my main, I want to call this struct or return, but how...
like if I say it directly like printf(newModel); it won't work, it says its undeclared I want to access the variables within the previous function. How do I do so.

This is C, not C++ just as a heads up...
I don't think getters/setters would work in C? like:
model* getModelVerts(){
return vertexBuffer;
}

would this work? or am I going about this all wrong?
1
2
3
4
newModel->vertexBuffer;
newModel->Faces_Triangles;
newModel->normals;
newModel->TotalConnectedTriangles;

These lines do nothing. Absolutely nothing. What are you trying to do here?

call this struct or return, but how...

You can't call a struct. You can only call functions. A struct is just another kind of variable you can create. What is your question? Are you asking how to create and use structs of type model?
I don't think getters/setters would work in C?

Of course they will. All a getter does is return some value. All a setter does is set some value. C code is chock full of setting values and reading them. However, since in C a struct does not have functions, you'd have to have stand-alone getter and setter functions, and since getter and setter functions exist to allow you to control access, it's pretty pointless in C as there is no concept of public or private.

A big part of the problem here is that you are thinking in terms of programming concepts that don't exist in C. You're clearly trying to create objects, with member functions, which C just doesn't do in nearly so neat a way as a programming language built with that in mind.
Last edited on
Well what I'm trying to do is this:
I'm reading in model data through my model.c
I want it to store the data in that model.c file so that I can call it in other functions.
I want it to store its vertexBuffer, Faces_triangles, normals and TotalConnectedTriangles.

So, lets say I load 3 different models. I want it to store the data uniquely for those three seperate models, and I want to be able to pull the data out of each one as I see fit.

like, for example, I have a drawing function that will draw the model that is loaded, but it needs the data that came from my model.c file. More specifically my model function.

I have my model data stored in my model function as vertexBuffer, Faces_triangles, normals and TotalConnectedTriangles. It DOES store the data, but I can pass it onto anything, or have it accessible.
Last edited on
I want it to store the data in that model.c file


This makes no sense at all. model.c is a text file that gets compiled to make your program.

Do you mean that you want to make an object of type struct model and store data in it?
Last edited on
I READ it in, using FILE *file = fopen(filename, "r");
I'm reading in .obj files through my model.c class

In my function, I read it in, split it up into pieces and assign them their own arrays to be read in through OpenGL.
So you create some variable of type struct model, and then you put the data in them. Great. What exactly are you having trouble with?
The thing is, I have the arrays loaded in, after the file has been read and split up in my function in model.c I just want to figure out a way that I can access this information (be it whatever technique it is, enums, structs, return values).

Like for example. I load up box.obj in my model.c class
it splits it up into vertexBuffer, Faces_triangles, normals and TotalConnectedTriangles.

what can *I* do to access these variables within this load function somewhere else, like lets say in main.c

I may not even need to use a struct, I just threw the struct in there because I thought I could store data in this struct and access it elsewhere.
Last edited on
what can *I* do to access these variables within this load function somewhere else, like lets say in main.c


Create the struct model variables in your main function (stop thinking about main.c - main.c does not exist once the program has been compiled). Pass them by pointer to whichever functions you have that read the data and fill them up, and then you can pass the filled-in struct around wherever you like from the main function.

I load up box.obj in my model.c class

No, you don't, because this in C, and C has no meaningful class type, and even if it did, model.c is a text file - not a class. If you're going to code in C, think in C. If you're thinking in classes, you'll go wrong.
Last edited on
Now will this work if I create three different instances of a model and will I be able to access each one uniquely to the other?

By the way, thank you for your help so far.
1
2
3
int a;
int b;
int c;


This makes three different variables. You can set the values of each independently:

1
2
3
a=7;
b=8;
c=9;


You can read a, b, and c independently, and you can set their values independently. Happy with this?

1
2
3
struct model d;
struct model e;
struct model f;


This makes three different variables. You can set the values of each independently:

1
2
3
d.TotalConnectedTriangles = 7;
e.TotalConnectedTriangles = 8;
f.TotalConnectedTriangles = 9;


When you make an instance of a struct, it exists independently of any other struct you made.
Last edited on
I believe that's what I did. I had my struct set up as mentioned earlier
and in my function to load, I create this:
model* newModel = (model*)malloc( sizeof(model) );

This creates a new model with the struct variables, correct?
If this is the case, then I can just push the data into it (once it has been read in and processed) like so:

newModel->vertexBuffer;
newModel->Faces_Triangles;
newModel->normals;
newModel->TotalConnectedTriangles;

So, wouldn't this fill the struct with the post-processed data?
I understand its called 'newModel' so if I were to call this function and load 3 different models, it would be just create newModel and overwrite it 3 times, correct?

I'm sorry if I'm being difficult, return values and getting data is something that's a little over my head in C, so I'm just trying to clear it up. The only reason why I mention different classe is to help people see where its being called from. It's a segmented system such as:
model.c
light.c
camera.c
...
all with their appropriate .h files as well

and a main.c
I understand its just a text file, but I just want to show where they are being put together. I don't want to write everything monolithically in one file.
I think part of this is also that you don't understand what malloc does. You need to read up on malloc.

Also,
1
2
3
4
newModel->vertexBuffer;
newModel->Faces_Triangles;
newModel->normals;
newModel->TotalConnectedTriangles;

does nothing. Absolutely nothing. What are you trying to do here? Assign values? Call functions? Whatever you're trying, it's not working. This does nothing.

I understand its called 'newModel' so if I were to call this function and load 3 different models, it would be just create newModel and overwrite it 3 times, correct?


No. If you load three different models, for example by giving them different names, when you assign values to one of them, the other two will not be overwritten. Here, look at this:

1
2
3
4
5
6
int a;
int b;
int c;
a=7;
b=8;
c=9;

See how I made three different int values, and then gave them values? When I gave a value to c, it did not overwrite the value given to a or b.

Last edited on
Topic archived. No new replies allowed.