#### Archived

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

# Stupid pointers

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

## Recommended Posts

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:  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[i][j].idY = ??? } } }  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[i][j].idX = &a and &b but when I do: printf("%d",world[i][j].idX); i get of course the address. If I do: world[i][j].idX = i; world[i][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

##### Share on other sites
you created a typedef for a structure and not a structure.

try:
*(world[j].idX) = i;
*(world[i][j].idY) = j;

##### Share on other sites
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.

##### Share on other sites
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

##### Share on other sites
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?

##### Share on other sites
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:
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)

Edited by - JungleJim on June 8, 2001 5:08:09 PM

##### Share on other sites
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.