# needs lots of help with arrays,pointers, and enumerators

Printable View

• 11-11-2001
ssjnamek
needs lots of help with arrays,pointers, and enumerators
ok ive looked at at arrays and pointers before as well as enumurators(i think thats how u spell it) and they just dont click in so i can understand it and i have really no idea what im doing with them or much less how to use them or read them and what there point is?could anyone help me with this and give me sum idea of how to use this stuff or what its for cause all it does is give me one big headache and i have no idea what im doing?
aight well ive gotta go so ill check back later bye and thanx for taking the time to read this and helping me ;)
• 11-11-2001
Betazep
Do you want to start with an array perhaps?

Imagine a small grid of boxes. Each box being about 1cm by 1cm.

Each of those boxes for our purposes equals a space to hold something, like perhaps an integer or a character.

Lets say we want several characters in a row so that we can grab them easily with referencing to the group in question (though realize this isn't how physical memory works persay... arrays are not always in a neat row).

Now take this array for example

char my_array[10];

This would put an array of 10 characters (0-9) somewhere in memory.... one in each of the blocks (from earlier).

At this time there is nothing but garbage in the character blocks, but the space is available to put things. Like...

my_array[3] = "C";

places the letter C in the fourth element of the array (you must remember that the first element is at position zero.)

Unfortunately, I am running out of time. State more specific questions in this thread, because for all I know... you may already know this much.

I will be back to help you as I am certain some of our other members will help you too.
• 11-11-2001
matheo917
i will continue on the array as the previous colleague did....

let' s take a different approach....

an Array is a collection, a group of same items.....for instance: think of a box of cigarets.... that box is an Array, it can be empty, it can consisting of 20 cigarets or even 1000 depending who created it...let's say you bought a pack of cigarets that is designed to hold 20 cigarets ==== that would be analogous to an Array that was declared to hold 20 items, let's say for instance integers,
like was written in the preceding post your pack of cigarettes (an Array) doesn't necessarily has to have it's full capacity (20 cigar.)
it could have 10 that are spread around in all different places...an Array, as well as a pack of cigarettes, IN THIS CASE, has 20 slots
that are numbered between 0 and 19 AND NOT, NOT, NOT
• 11-11-2001
matheo917
SORRY DIDN'T GET TO FINISH

... NOT from 1 to 20.....

you declare and array by first writing the type that it will hold, then the name of it, and then inside of brackets you put how many of these items it will hold: for example:

int Cigarettes[20];

what you just did is created an Array (group, box) of cigarettes (integers in this cas) , it is an empty box that can hold 20 cigaretes.....

HINT:::: don't forget those slots are numbered between 0 and 19, which is still 20 slots.....

You can put cigarettes in those slots in which ever order you want

you can put and integer which is, let's say 123 in the third slot...

Cigarettes[2] = 123;

by doing so you have accessed the third slot and put integer 123 in that area..... don't forget [2] is actually a third number cell and not second........

i hope that helpssssssss.....

let us know

Regards,
matheo917
• 11-11-2001
ssjnamek
hey thanx that kinda cleared up the declaring and assigning arrays and how to use it but now the part i have no idea of how to deal with use reconize and understand is(hope thats speccific enough lol) pointers and enumerators
• 11-13-2001
GorGorAlmighty
Pointers:

A pointer is a variable that holds a memory address.

If you have an integer variable called var:

int var = 10;

You can initialize a pointer that will 'point' to the address of var by:

int * pPointer = 0;
pPointer = &var;

"int * pPointer = 0;" initializes the pointer and "pPointer = &var;" stores the 'address of' var (& is the address of operator).

Note:
int* pPointer = 0; is the same as
int * pPointer = 0; is the same as
int *pPointer = 0;

So now, pPointer contains the 'address of' var.

You can alter the contents of var indirectly by changing the contents of *pPointer:

*pPointer = 99;

Now var contains the value 99 while pPointer (not the same as *pPointer) still contains the 'address of' var.

This all seems kind of pointless until you start working with 'advanced' functions. Functions can only return a single value unless you use pointers. This requires much further explanation. Let us know if you'd like us to go into it.

A great book to get is "Teach Yourself C++ in 24 Hours". It's a SAM's publication and it goes into all this stuff. I paid \$19.99 and it comes with the Borland compiler.
• 11-17-2001
ssjnamek
ok that kinda helps i half way understand pointers now but yea im kinda at thepoint where i have simple functions prettywell and im gong towards more complex functions but i need to know pointers array and enumerators pretty well right? and the book im using is c++ primier plus or something like that anyway but yea that would help if u explained more of why u need to kno this since i learn things better if there is a reason behind it all OK? well thanx and ill check back later i jus forgot i had a thng posted and remebered today so sorry if it took so long to reply aight well look forward to hearing from u again bye
• 11-17-2001
Unregistered
I haven't seen enumeration used that much. It's normally used to make a program more readable.
The next paragraph states enums are data types, which is not true. But it can help think of them that way to learn them. Just remember that they aren't actual data types, just nicknames for integers.
When you create enumerations, think of the enum keyword as a command to create a data type. You name the data type, and assign it allowed values:

enum Pets{dog, cat, mouse};
Pets fido;
fido = dog;

What you are doing is creating a data type called Pets. Pets is a data type like int, char, double, etc (not actually, see above). You create a variable of type Pets, fido in this case. The allowed values that any variable of type Pets can have is dog, cat, or mouse.
Remember, enum doesn't actually creat a data type. The values dog, cat, mouse are all aliases, or nicknames, for integers. Unless you specify otherwise, dog = 0, cat = 1, mouse = 3, etc.
The enum helps in reading your code, instead of testing for integer values directly.
• 11-17-2001
GorGorAlmighty
Pointers for functions:

I'll explain it in 'C' terms since I think it's easier to understand...

A function can only return one value...or can it? Hmmm....hello pointers!

initialize variables:

int myAge = 0;
int myHeight = 0;
int myWeight = 0;

now, the function prototype so the function knows what types of variables to expect:

void thisFunction(int *, int *, int *);

The function 'thisFunction' expects 3 pointers. You don't have to name the function variables in the prototype. Notice that the function returns NO value (void).

Now, the function:

void thisFunction(int * pAge, int * pHeight, int * pWeight)
{
*pAge = 27;
*pHeight = 72;
*pWeight = 168;
}

The function is passed 3 pointers, alters the values, and then returns nothing. The names of the pointers can anything you want, as long as they're consistent inside the function.

Now call the function:

main()
{

//call the function, passing the ADDRESSES of the variables...
thisFunction(&myAge,&myHeight,&myWeight);

}

When calling the function from main, you pass it the 'address of (&)' the 3 integers you want to alter. Therefore, when the function alters the variables, it is actually pointing to the address of the variables and modifies the data at that address.

Notice that the function returns no value, but is actually returning 3! That's what makes pointers so useful.
• 11-17-2001
ssjnamek
ok im totally lost about those enumarators and i understand now what a pointer does but how do i actually use it im still a little confused on
• 11-17-2001
Estauns
Let me give a shot on pointers here,

Basically pointers are just memory saving conventions, instead of having one billion variables to hold a players name, you have one variable and a pointer to that address, so that a million functions can use that.

I'm going to relate this to games, since games are fun. You have a player in your game, and that player is assigned a name. For arguments sake, lets call him Joe.

You have a pointer, point to the variable which stores that players name. Now you can use that pointer in your functions, so you can pass the memory address itself (1 byte I think), instead of the actual value, which could be 1, 2, 16, 32, bytes of memory. This will quickly save time, because imagine you have to show that players name 76 times a second... Which would you rather do? Pass 1 byte (the pointer) or 32 bytes (the name itself) to a function?

See how much faster that is? Hope that helped.
• 11-18-2001
Brown Drake
Quote:

enum Pets{dog, cat, mouse};
Pets fido;
fido = dog;

Try this for an analogy. Pretend there is no int data type, so you create one:
create data type int {-32767...32767};
int x;
x = 32767;
Like the quoted post originally said, enums are not separate data types (like int, double, etc), but to get a grasp on them you can think of them that way.Using = below to mean "is similar to", then in the analogy,
enum = create data type
Pets = int
{dog, cat, mouse} = {-32767...32767}
fido = x
dog = 32767
enum "creates" the data type Pets, the way "create data type" creates the data type int.
{dog...} gives the allowed values for data type Pets the way {32767...} gives the allowed values for data type int.
fido is a variable of type Pets the way x is a variable of type int.
The values fido can have are dog, cat, or mouse, the way x can have a value from -32767 to +32767.
Got that part? Ok.
Then, to say enums are really integers, that they're just nicknames or aliases, is to say that enum Pets{dog, cat, mouse}, Pets fido, fido = dog is the same as saying int fido = 0. If you're working with a program involving pets, maybe for a vet, it can make your code easier to read if you write fido instead of x, and dog or cat instead of 0 or 1. In an if statement, for example, if (fido = dog) may be clearer than if (x = 0). You never have to use enums.
One other value they have is restricting the values you can use. If you used int, you might get somewhere an erroneous value of 5 for x. But say values for x in your program should never go over 2. Using enum, you have no value for 5. In fact, 5 in the form of an int is not an allowed value for Pets.
Hope that helps.
• 11-18-2001
Salem
This is a good pointer tutorial
http://pweb.netcom.com/~tjensen/ptr/pointers.htm
• 11-30-2001
Unregistered
i dun quite undrstand how that last part u said about enumerators where u can restrict values since i kno how to do that with an if statement of dont accept this value or w/e but i didnt quite get that all about enumerators but u said i dont ever have to use them? so i really dun need to bother with them?