Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

fishleg2

help with pointers

This topic is 5272 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Im trying to malloc an array of pointers because i dont know how many objects it will be pointing to, struct, typedef struct tagTriMesh { float *Vertex_Arr; float *Normal_Arr; float *Tex_Arr; int *Inds_Arr; int numtri; int numvert; struct tagTriMesh *Children; //Grrr Wont Work int numchildren; } TriMesh; func in question, TriMesh *TriMesh_Load(const char *filename); This works if i go, TriMesh *cube = TriMesh_Load("cube.txt"); This wont work when i malloc the children it wont let me use TriMesh_Load incopatible types even tho they arent, New->Children = (TriMesh*)malloc(New->numchildren * sizeof(TriMesh)); then i loop round numchildren for(iChildren = TriMesh_Load("cube.txt"); //line i get prob that i line i get '=' : left operand must be l-value i dont know what i means. Thx for any help with this, [edited by - fishleg2 on April 13, 2004 12:12:59 PM] [edited by - fishleg2 on April 13, 2004 12:13:38 PM]

Share this post


Link to post
Share on other sites
Advertisement
Problem one: You''re allocating an array of TriMesh structs instead of an array of pointers to TriMesh structs.
You need to change:
(TriMesh*)malloc(New->numchildren * sizeof(TriMesh));
to:
(TriMesh**)malloc(New->numchildren * sizeof(TriMesh *));

Also you''ll need to change your definition of Children to

struct tagTriMesh ** Children;

Problem two: You can''t change the address of a variable (which is what you''re trying to do when you assign a value to &New->Children). What you probably want to do is:

New->Children[i] = TriMesh_Load("cube.txt");

(Once you''ve change the definition of the Children variable anyways.)

Share this post


Link to post
Share on other sites
thx alot for help super fast aswell,

wondering if you could help me some more with pointers im trying to read in a string from a text file then pass it to that TriMesh_Load func,

i do this,
char *temp = 0;
fscanf(fp, "Childname : %d", &sizestr);
temp = malloc((sizestr) * sizeof(char));
fscanf(fp, " %s\n",&temp);

New->Children = TriMesh_Load(&temp);

free(temp); //dies here

i want to free the memory after its finished loading the objects but it dies there.

thx for any help,

p.s im also having trouble with fscanf

i want it to scan in,
fscanf(fp, "Childname : %d", &sizestr);
temp = malloc((sizestr) * sizeof(char));
fscanf(fp, " %s\n",&temp);
but if numofchildren is 0 i need it to skip that whole line i tried fscanf(fp, "\n",);
but then all the rest of the data becomes strange...

[edited by - fishleg2 on April 13, 2004 1:02:37 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by fishleg2
char *temp = 0;
fscanf(fp, "Childname : %d", &sizestr);
temp = malloc((sizestr) * sizeof(char));
fscanf(fp, " %s\n",&temp);

New->Children[i] = TriMesh_Load(&temp);

free(temp); //dies here


I think you want fscanf(fp, "%s\n", temp); (No &).

And I'm not really sure what you're asking in the second part.

edit: formatting

[edited by - SiCrane on April 13, 2004 1:07:01 PM]

Share this post


Link to post
Share on other sites
I figured why it crashed when freeing the string i needed
(char *)malloc (numofchars +1) some one explain why i need the +1 for i thought would give me 10 chars when ive only got 9...

my file looks like this,

NumChildren : 0
Childname : 9 Cube2.txt
VertexCount 24

if the numchildren is 0 i need it to go forward a line to get to vertexcount. not sure how to do it..



[edited by - fishleg2 on April 13, 2004 4:32:05 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by fishleg2
if the numchildren is 0 i need it to go forward a line to get to vertexcount. not sure how to do it..


Read the file into memory and then parse it for newlines.

Share this post


Link to post
Share on other sites
thx for help so far, not totally sure what you mean by load in the whole file at once tho.

if i have TriMesh ** T
how do i convert it to *T is it

so if i had,
TriMesh ** T
TriMesh *R
R = &*T


[edited by - fishleg2 on April 13, 2004 6:02:44 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!