|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Member (8 bit)
Join Date: May 2000
Posts: 219
|
Can someone tell me what the similarities and differences between a struct and an array is? this question is really boggling me. Thank you.
|
|
|
|
|
|
#2 |
|
Member (14 bit)
|
Hello,
a struct is something that can hold different variables. It doesn't matter whether the variables are of the same type or not. struct blah { int var1; int var2; float var3; }; So in this case the struct called "blah" holds 3 variables, and you can access them by typing the struct's name, a point and then the variable name. First of all we need a variable of that struct. blah testvar; And now we can access the sub-var easily. testvar.var2 = 1; So a struct is someting which helps you to organize variables which belong to one big thing. For example, if you want to program something that has to deal with people, you'll need information about that person. So therefore it'll be the best way to create a struct called person struct person { char name[20]; char address[40]; char sex; short age; //etc. }; The big advantage is that you can create as many people as you want, e.g. person RJ; person MS; //etc. but the sub-variables always have the same name. RJ.name = "Ralf"; RJ.sex = 'm'; RJ.age = 19; MS.name = "Miriam"; MS.sex = 'f'; MS.age = 20; I hope it's clear enough to understand what structs are and how they work. Now to the array: An array is a field of variables. There are also more dimensions for it. I begin with the one dimensional array. It's - more or less - a simple row of variables, like in lotto from 1 to 6. int lottonumbers[6]; So lottonumbers is an array of int, it consists of 6 int variables, numbered from 1 to 5. You can't give them names, you just access it by the index. lottonumbers[4] = 34; If you look at the definition of lottonumbers, you see how the array is defined. When you look now up to the struct, do you notice something ? Well, the string is defined just the same, isn't it ? Yeah, that's because a string is simply an array of char. You understand ? OK, the two-dimensional-array: If you look at the M$-game MineSweeper, the fiels is a two-dimensional array. int minesweeperfield[10, 10]; You access it also via index. This one is 10 fields width, and 10 fields height. I hope it was understandable. RJ [Edited by RJ on 03-24-2001 at 05:30 PM]
__________________
All's right with the world when your PC is working right.
|
|
|
|
|
|
#3 |
|
Member (8 bit)
Join Date: May 2000
Posts: 219
|
Thanks mate, that cleared everything up
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|