here are my type definitions:
//A grid location
typedef struct gridLocation {
object *placedObject;
int *idX;
int *idY;
};
//Initialize a grid to its desired size
void grid::createGrid(int x, int y) {
world = new gridLocation*[x];
for (int a = 0; a < y; a++) world[x] = new gridLocation[y];
for (int j = 0; j < gridY; j++) {
for (int i = 0; i < gridX; i++) {
world[j].idX = ???
world[j].idY = ???
}
}
}
</CODE>
Basicaly I forgot how to assign a static value to these variables. I''ve tried creating (witin the for loop) int a, b; and assigning world[j].idX = &a and &b but when I do:
printf("%d",world[j].idX); i get of course the address. If I do:
world[j].idX = i;
world[j].idY = j;
i get errors because the type is wrong.
This is some dumn thing, but I am working on so much stuff right now, I figured someone would remember off hand.
Thanks in advance
</i>
Stupid pointers
O.k, I should know why this doesn''t work, but i don''t. I always hated complex pointer stuff. Here goes:
I want to have a 2D dynamic array of pointers to an object. For testing purposes I have a 2D dynamic array of pointers to ints. I can''t seem to get assignents to work, here''s my code:
you created a typedef for a structure and not a structure.
try:
*(world[j].idX) = i;
*(world[j].idY) = j;<br><br> </i>
try:
*(world[j].idX) = i;
*(world[j].idY) = j;<br><br> </i>
If you want to have the address of a variabele, you should put an ampersand (&) before it. And I wouldn''t assign the pointers to i and j (or a and b) from your examples to idX and idY because these variables will eventually be destroyed. You should either create some integers dynamically or make idX and idY normal integers.
about the structure thing, you need to do the following as well
typedef struct gridLocationStruct {
object *placedObject;
int *idX;
int *idY;
} gridLocation;
or else you''ll have compiler errors
typedef struct gridLocationStruct {
object *placedObject;
int *idX;
int *idY;
} gridLocation;
or else you''ll have compiler errors
It''ll just generate a warning, no errors.
And how did you declare ''world''? As a pointer to gridLocation or as a pointer to an array of gridLocations?
And how did you declare ''world''? As a pointer to gridLocation or as a pointer to an array of gridLocations?
I'd like to say one thing on struct definitions, because I often see a myriad of ways how people actually use them. It may seem like nitpicking, but I'd like to say it anyway
You have to distinguish the rules for C and C++, because they are not the same!
In C++, you define a struct as follows:
To define a variable of type struct_name, you do
Or, you could do the struct and variable definition in one single time:
No need for typedefs!
In C, it's different. To define a structure, you have to use
Looks the same as the C++ way, doesn't it? However, to define a variable of that type, you have to use
Notice the extra struct keyword! So the type of var_name isn't 'struct_name_t', but it's 'struct struct_name_t'. Because that's a lot of extra typing, often a typedef is used:
'struct_name' becomes a typedef for 'struct struct_name_t', which saves you from typing the struct keyword. This mean you can define a variable as follows:
If you have the typedef, you usually do not use struct_name_t anymore (struct_name_t is actually called the tag). To leave out the tag in the typedef, you can use an anonymous struct definition:
You cannot refer to the actual struct type, only to the typedef.
Because Visual C++ creates .cpp files as default C/C++ files, the compiler will always compile these files in C++ mode. This means you can use the C++ way of defining structs. If you would rename the source file to have a .c extension (or use the /Tc compiler option), you can only use the C way of defining structs. (I didn't actually check this, btw)
Thank you for your attention
Edited by - JungleJim on June 8, 2001 5:08:09 PM
You have to distinguish the rules for C and C++, because they are not the same!
In C++, you define a struct as follows:
struct struct_name { // ... };
To define a variable of type struct_name, you do
struct_name var_name;
Or, you could do the struct and variable definition in one single time:
struct struct_name { // ... } var_name;
No need for typedefs!
In C, it's different. To define a structure, you have to use
struct struct_name_t { // ... };
Looks the same as the C++ way, doesn't it? However, to define a variable of that type, you have to use
struct struct_name_t var_name;
Notice the extra struct keyword! So the type of var_name isn't 'struct_name_t', but it's 'struct struct_name_t'. Because that's a lot of extra typing, often a typedef is used:
struct struct_name_t { // ... } struct_name;
'struct_name' becomes a typedef for 'struct struct_name_t', which saves you from typing the struct keyword. This mean you can define a variable as follows:
struct_name var_name;
If you have the typedef, you usually do not use struct_name_t anymore (struct_name_t is actually called the tag). To leave out the tag in the typedef, you can use an anonymous struct definition:
typedef struct { // ... } struct_name;
You cannot refer to the actual struct type, only to the typedef.
Because Visual C++ creates .cpp files as default C/C++ files, the compiler will always compile these files in C++ mode. This means you can use the C++ way of defining structs. If you would rename the source file to have a .c extension (or use the /Tc compiler option), you can only use the C way of defining structs. (I didn't actually check this, btw)
Thank you for your attention
Edited by - JungleJim on June 8, 2001 5:08:09 PM
Thanks about the struct help. I code in C only, this is the first time in a while I''ve coded C++, but the project I''m working on requires it.
About world, i have defined world as:
gridLocation **world;
I don''t really care about the ints, (used just to test if it works or not) but I am creating objects of type object elswhere, and need to move them around in the array world. The struct definition is not c++ (i''ve fixed it) but it''s not wrong. However I still need a way to assign properly to it, and read properly from it.
About world, i have defined world as:
gridLocation **world;
I don''t really care about the ints, (used just to test if it works or not) but I am creating objects of type object elswhere, and need to move them around in the array world. The struct definition is not c++ (i''ve fixed it) but it''s not wrong. However I still need a way to assign properly to it, and read properly from it.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement