pointers, malloc help

Started by
5 comments, last by outRider 23 years, 7 months ago
I''m having some problems allocating memory for a pointer. Heres what I have so far. typedef struct tagVERTEX { float x, y, z, u, v; } VERTEX; typedef struct tagTRIANGLE { VERTEX vertex[3]; } TRIANGLE; typedef struct tagSECTOR { int nTriangles; TRIANGLE *triangle; } SECTOR; void ReadStr(FILE *file, char *string) { do { fgets(string, 255, file); } while ((string[0] == ''/'') || (string[0] == ''\n'')); return; } void LoadSector(char *SectorFile, SECTOR *sector) { int nTriangles, loop, vert; char cTextLine[255]; FILE *FileIn; FileIn = fopen(SectorFile, "rt"); ReadStr(FileIn, cTextLine); sscanf(cTextLine, "#P %d\n", &nTriangles); sector.triangle = malloc(TRIANGLE[nTriangles]); sector.nTriangles = nTriangles; for (loop = 0; loop < nTriangles; loop++) { for (vert = 0; vert < 3; vert++) { ReadStr(FileIn, cTextLine); sscanf(cTextLine, "%f %f %f %f %f", sector.triangle[loop].vertex[vert].x, sector.triangle[loop].vertex[vert].y, sector.triangle[loop].vertex[vert].z, sector.triangle[loop].vertex[vert].u, sector.triangle[loop].vertex[vert].v); } } fclose(FileIn); return; } The original code was written using new instead of malloc, so I might be doing something wrong here, but I don''t know what. Any help would be appreciated. The errors I get are as follows: Error E2294 C:\source\main.c 68: Structure required on left side of . or .* in function LoadSector Error E2108 C:\source\main.c 68: Improper use of typedef ''TRIANGLE'' in function LoadSector Error E2294 C:\source\main.c 69: Structure required on left side of . or .* in function LoadSector Error E2294 C:\source\main.c 76: Structure required on left side of . or .* in function LoadSector Error E2294 C:\source\main.c 76: Structure required on left side of . or .* in function LoadSector Error E2294 C:\source\main.c 77: Structure required on left side of . or .* in function LoadSector Error E2294 C:\source\main.c 78: Structure required on left side of . or .* in function LoadSector Error E2294 C:\source\main.c 79: Structure required on left side of . or .* in function LoadSector ------------ - outRider -
Advertisement
You''re passing sector in as a pointer but your not using it
like a pointer.

Your code that looks like:

sector.triangle = malloc(TRIANGLE[nTriangles]);
sector.nTriangles = nTriangles;

Should use the "->" operator and not the "." operator.

Change it to:

sector->triangle = malloc(TRIANGLE[nTriangles]);
sector->nTriangles = nTriangles;

Same thing with pointer used elsewhere.

// CHRIS
// CHRIS [win32mfc]
Hi,
Is the line triangles = malloc(TRIANGLE[nTriangle]);
correct?
is TRIANGLE an array of sizes?
ifnot u might want to do something like,
if triangle was a struct that u wanted to make and
array from,

traingles = (struct triangles *)malloc(sizeof(struct triangles)*nTriangle);

Evan,
evan@achaia.dircon.co.uk
Yep,
ignore most of that last post!
what your other problem not pointed out by the first reply to ur post, is that the malloc field isn''t used correctly,
you want:

sector->triangle = (TRIANGLE *)malloc(sizeof(TRIANGLE)*nTriangles);

I think u must of been confused cause of the way the new keyword works in C++ (don''t understand it my self)
somming like new [ARRAYNUMBER] TYPE
??
Evan,
evan@achaia.dircon.co.uk
(not that it has anything to do with the original post, but it felt like someone was asking about the new operator, maybe?)

to allocate memory for an array of structs using the new-operator you write:

new TYPE[NumberOfItems]



I''m not entirely sure I understand why you would rather use malloc than new. The new operator is simpler to use, and much, much safer than malloc.
My suggestion is toss the malloc, and use new.

-Neophyte

~Death awaits you all, with sharp, pointy, big teeth~
The new operator is simpler than malloc? Not really, malloc is just a function call with how much you wanna allocate. Then you check for errors. New may be less buggy (although i never heard of a malloc bug, please feel free to inform me so I can improve my habits), but not really much simpeler

-----------------------------

A wise man once said "A person with half a clue is more dangerous than a person with or without one."
-----------------------------A wise man once said "A person with half a clue is more dangerous than a person with or without one."The Micro$haft BSOD T-Shirt

This topic is closed to new replies.

Advertisement