Skip to main content
GameDev.net gamedev.net
🔒 Locked

structure and array

Started by Alpha Brain Jun 29, 2006 at 10:58 PM 11 replies 1.4k views
Original Post
Alpha Brain
Alpha Brain
According to the following code, how do I get an element from test[3]??? It's the first time I'm seeing this kind of stuff, so I don't really know what's happening... ----------------------------------------------------- #include using namespace std; typedef struct tagStructure{ int tableau[4]; } STRUCTURE; int main(void) { STRUCTURE test[3] = { {2, 2, 3, 5}, {6, 7, 3, 1}, {5, 8, 2, 4} }; return 0; } -------------------------------------------------------- I tried cout << test[2][3], but it didn't seem to work. [Edited by - Alpha Brain on June 29, 2006 11:55:18 PM]
Doggan
Doggan
You are creating an array of size 3, containing STRUCTUREs. So you would access an element from your array as such:

test[ x ].tableau[ y ], where x is from 0 to 2, and y is from 0 to 3.
Alpha_ProgDes
Alpha_ProgDes
i believe you should be doing this:
test[2].tableau[3]

also, i think that you initialized your STRUCTURE wrong only because of the way your struct is setup.

it also i believe should go like this:
int list_of_numbers[] = {2, 2, 3, 5, 6, 7, 3, 1, 5, 8, 2, 4};void init_STRUCTURE (int array[]){   int i, j;   for (i = 0; i < 3; ++i) {     for (j = 0; j < 4; ++j) {        test.tableau[j] = array[i*4 + j]; //see explanation below     }   }}


it took so long to post because i was trying to remember the formula. anyway to turn a one-dimensional into a two-dimensional array you have to use that formula. basically you take the number of the row you're on and number multiply that by the number of element to give the right (y,x) element and add the index. for instance, if I wanted to get to 6 in the STRUCTURE I take the row (which is i = 1, because rows and columns in C/C++ start at 0) and multiply with the number of elements (which is 4. so at point I have i * 4 = 4. now I'm at element 4.) and finally add the index number (which is j = 0, because 6 is at index 0. so I have i * 4 + j = 1 * 4 + 0 = 4).

so now that I've confused you with my horrible explanation. just take my word that the formula works. [smile] thanks, bye.
Beginner in Game Development?  Read here. And read here.  
Alpha Brain
Alpha Brain
another question based on the same code from above;

what does the following typedef mean?

typedef STRUCTURE BLOCK[512]

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


#include

using namespace std;

typedef struct tagStructure{
int tableau[4];
} STRUCTURE;

typedef STRUCTURE BLOCK[512]

int main(void)
{
STRUCTURE test[3] = {
{2, 2, 3, 5},
{6, 7, 3, 1},
{5, 8, 2, 4}
};

return 0;
}
------------------------------------------------------------------
Trienco
Trienco
Quote:
Original post by Alpha Brain
typedef struct tagStructure{
int tableau[4];
} STRUCTURE;


Who wrote this anyway? Either somebody needs to clue me in on the whole point of this or this code is coming from some C-coder that switched to C++ and just couldn't accept that typedef has become absolutely useless (except for making code look nicer).

To me that looks like he is at the same time declaring struct tagStructure{} in the C++ way AND typedef struct {} STRUCTURE in the C-way (or rather, typedef tagStructure STRUCTURE). If that's the case you should really find another source for your example code.

Quote:
typedef STRUCTURE BLOCK[512]


It's looking silly, but should simply mean that BLOCK x is now a nicer way of saying STRUCTURE x[512]. Why on earth did they even decide on this way in the first place?
Was it someone saying "noooo, 'Type[N] name' would SO keep me from multiple declarations in one line" or is there a more meaningful reason for it? It pretty much keeps you from saying "this is an array of type X" without introducing some dummy name.
f@dzhttp://festini.device-zero.de
kvp
kvp
Quote:
Original post by Trienco
It's looking silly, but should simply mean that BLOCK x is now a nicer way of saying STRUCTURE x[512]. Why on earth did they even decide on this way in the first place?
Was it someone saying "noooo, 'Type[N] name' would SO keep me from multiple declarations in one line" or is there a more meaningful reason for it? It pretty much keeps you from saying "this is an array of type X" without introducing some dummy name.


A good example where this can be useful is when you have a structure that is defined by a library and you plan to change it in the future. To avoid rewriting every use of the library, you can typedef it.

Example:
typedef struct
{
char letter;
}LETTER;

typedef LETTER PASSWORD[64];

The user of the lib might write:

PASSWORD pass;

When you want to change it, you can write:

typedef struct
{
w_char letter;
}LETTER;

typedef LETTER PASSWORD[256];

And the user of the lib can leave it as:

PASSWORD pass;

As long as the users don't hardcode the size into their program, they don't have to change anything, since the number of letters (inclusive terminating 0) will remain: sizeof(PASSWORD)/sizeof(LETTER)

Of course this magic is needed only when someone codes in old school c...
Trienco
Trienco
Quote:
Original post by kvp
A good example where this can be useful is when you have a structure that is defined by a library and you plan to change it in the future. To avoid rewriting every use of the library, you can typedef it.


True, but the question was: why did the settle for int x[10] instead of int[10] x? One would think that making the array part of the type is more intuitive. So why did they make it part of the variable name?

The only reason I could think of right now is notorious "squeeze everything into a single line"-coders went postal over the outlook of not being able to do:
int x, *y, z[12];

For your example it wouldn't matter if it's
typedef LETTER PASSWORD[N]
or
typedef LETTER[N] PASSWORD

f@dzhttp://festini.device-zero.de
ZQJ
ZQJ
I believe that should actually be:

STRUCTURE test[3] = {  {{2, 2, 3, 5}},  {{6, 7, 3, 1}},  {{5, 8, 2, 4}}};


Because there is an array inside the structure which requires the extra {} to initalize. I haven't checked and I may be wrong but IIRC gcc won't accept the original code, although MSVC will as an extension, but it's nonstandard.

Zahlman
Zahlman
Quote:
Original post by Trienco
For your example it wouldn't matter if it's
typedef LETTER PASSWORD[N]
or
typedef LETTER[N] PASSWORD


Except that the latter, AFAIK, doesn't actually work, even though it's the one that looks like it should work in favour of the other. :( This is presumably to stay consistent with the rest of the syntax.

Meanwhile - Java doesn't have typedef, but for array declarations it lets you do them *both* ways, which can lead to some confusion. It also lets you declare 2-d arrays with both dimensions specified, even though the resulting array is not rectangular, but an array of array objects. And although a Foo[][] ought to be an array of Foo[]s, it's not parsed as (Foo[])[] - which is to say, you have to write Foo[5][] when Foo[][5] seems more logical to me.

It just goes to show you that the people responsible for language syntax clearly are not the ones who have to use it :)




To the OP: you can overload the operator[] to provide the syntax you want.

struct STRUCTURE {  int tableau[4];  int& operator[size_t x] { return tableau[x] };  const int& operator[size_t x] const { return tableau[x] };};
Alpha Brain
Alpha Brain
Thanks!

On the follow code I'm creating a new type using 16 bytes. Then I'm creating an array and I'm trying to cout the elements inside the array. My question is, why the memory adresses are printed instead of the elements?

(In case you're wondering, I'm dealing with chunk of memory with GBA programming, so I'm just trying to understand that stuff correctly before doing more work with the GBA.)

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#include

using namespace std;

int main(void)
{
// create a 16bytes type
typedef unsigned int int16[4];

// check the size of int16
cout << sizeof(int16) << endl << endl;

// create an array with int16 type
int16 array[2][2] = {
{{6, 2}},
{{3, 4}}
};

for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j ++)
{

cout << array[j] << endl << endl;
}

return 0;
}
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Zahlman
Zahlman
Quote:
Original post by Alpha Brain
Thanks!

On the follow code I'm creating a new type using 16 bytes. Then I'm creating an array and I'm trying to cout the elements inside the array. My question is, why the memory adresses are printed instead of the elements?


Because each "element" is in turn an array of ints, and IO streams don't provide any special handling for printing arrays (it's a bit difficult to do that anyway when just a pointer will be passed to the printing function ;\)

You are strongly recommended to use a structure that wraps an array. You can write your own, but you're likely to miss something useful - so take a look at boost::array. [boost] [smile]

Topic Locked

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

Sign in to reply to this topic.