Skip to main content
gamedev.net
🔒 Locked

C++ Structs and Array question

Started by Hungry Joe Jan 8, 2003 at 1:51 PM 5 replies 1k views
Original Post
Hungry Joe
Hungry Joe
i need to create an array of doubles, of dimensions [3][n] at the moment I am using 3 separate arrays like so: __________________________________ n = 23; double *x; x = (double *) new double[n]; double *y; y = (double *) new double[n]; double *z; z = (double *) new double[n]; __________________________________ what would make it much better would be if I could make an array of structs called, say, testPoints but I dont really know how to do that... can I just declare a struct in my header file like so __________________________________ struct TESTPOINTS{ double x =0; double y =0; double z =0; } __________________________________ then write: __________________________________ TESTPOINTS *points; points = (TESTPOINTS *) new TESTPOINTS[n]; _______________________ and if someone could tell me what a TYPEDEF means i would love you lots! thanks Joe
0110011001110101011000110110101100100000011011110110011001100110
djsteffey
djsteffey
you dont really need typedef for c++


  
struct TESTPOINTS
{
double x;
double y;
double z;
};
.
.
.
.
TESTPOINTS* points;
points = new TESTPOINTS[n];




"I pity the fool, thug, or soul who tries to take over the world, then goes home crying to his momma."
- Mr. T
CpMan
CpMan
quote:
Original post by ncsu121978
you dont really need typedef for c++

Nonsense. Who told you that. It''s incredibly useful for reducing code length in some cases, aliasing names together (how do you think you think the standard library does some things). Furthermore, if you''re doing things like template metapogramming, it''s neccesary in many cases.




Gamedev for learning.
libGDN for putting it all together.
An opensource, cross platform, cross API game development library.
VSEDebug Visual Studio.NET Add-In. Enhances debugging in ways never thought possible.
Fruny
Fruny
Yes.


struct TESTPOINTS
{
double x,y,z; // no default initialisers.
}

TESTPOINTS *points;
points = new TESTPOINTS[n]; // no need for a cast, new return ''the right type''.

delete[] point; // use the array version of delete to match new[]


A typedef creates an alias for a type : both names refer to the same type. Syntax is the same as declaring a variable, except with typedef in front.

typedef TESTPOINT Foo; // Foo is now the same type as TESTPOINT.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Hungry Joe
Hungry Joe
thanks folks! you''ve been a great help!
0110011001110101011000110110101100100000011011110110011001100110
djsteffey
djsteffey
sorry....i was thinking of #define
they suggest to use const instead of #define for c++

example

  
#define MAX_POINTS 100

or

const int MAX_POINTS = 100;



they recommend the 2nd way for c++....not sure why...something about the compiler knowing it is a type or something.
Although i use the first method....since it will save on memory a little bit.

"I pity the fool, thug, or soul who tries to take over the world, then goes home crying to his momma."
- Mr. T
mond
mond
quote:
Although i use the first method....since it will save on memory a little bit.

Unless you''re writing C++ code for a wristwatch, I wouldn''t worry too much about the memory you''re using by switching from #define to const declarations. Pick the route that gives you the most robust code, then optimize from there if you find the need.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.