Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 03-24-2001, 02:42 PM   #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.
copyright_1978 is offline   Reply With Quote
Old 03-24-2001, 03:09 PM   #2
RJ
Member (14 bit)
 
Join Date: Feb 2000
Location: Offenbach/Main (Germany)
Posts: 8,485
Send a message via ICQ to RJ
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.
RJ is offline   Reply With Quote
Old 03-25-2001, 03:22 AM   #3
Member (8 bit)
 
Join Date: May 2000
Posts: 219
Cool Thanks

Thanks mate, that cleared everything up
copyright_1978 is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 06:51 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2