Arrays

So my teacher sucks at explaining things in class. We recently went over arrays and pointers. Can someone give me a basic run through that is easy to understand on these two points please?

I'd appreciate it. Thanks!
yes. Arrays are basically memory allocations. What an array does is allow you to store larg amounts of data in an organized way. Be careful though, you can use a whole lot of memory with multi or even single deminsional arrays (which would not be good for your computer).

use this: http://www.cplusplus.com/doc/tutorial/arrays/

It explains arrays better than i can.
An array is just a container that holds x amount of elements, and x must be known at compile time. Arrays are really a C thing, you should be using vectors if you're programming in C++. Well, C++11 has some new array deal but don't worry about that.
http://www.cplusplus.com/doc/tutorial/arrays/

Pointers just point to a place in memory.
http://www.cplusplus.com/doc/tutorial/pointers/
http://www.cplusplus.com/articles/EN3hAqkS/

The second link for pointers is a really good article written by a member of the forum.
closed account (zb0S216C)
Andrew2172 wrote:
So my teacher sucks at explaining things in class.

I laughed :)

Andrew2172 wrote:
Can someone give me a basic run through that is easy to understand on these two points please?

Sure. First: Arrays.

An array is a group of something, such as integers or characters. They are stored in memory -- one next to the other, with no spacing between them. Each object within the array is called an element. Each elements has an index. An index is a number that ranges from 0 (zero) to n-1 (n is the length of the array). The reason 1 was deducted from n was because computers count from 0 (zero). Effectively, 0 (zero) becomes 1. For example, if I had an array of 10 elements, the index of the last element will be 9.

Declaring an Array


Declaring an array takes this form: <Type> <Name>[<Size>]. Here, <Type> is type of data the array will hold. <Name> is obviously the name of the array. <Size> is the amount of elements of <Type> the array will hold. Here's a real-world example:

 
int My_array[5];

This array holds 5 ints. But how do you access the elements? It's straight forward. What you have to do is state the index of the element you want to access. For example, say you had an array of 10 elements, and you wanted the 4th. You'd takes the element number and reduce it by 1; this will give you the index required to access the elements. Here's a working example:

1
2
3
4
int My_array[10] = { 0 };

// Access the array.
My_array[(4 - 1)];

In the above code, I've simplified it. First I declared an array. But what's the part after it? That's the initialiser list. It's used to assign each element a value. However, when it's used in the same way as above, it means all elements of the array are set to 0 (zero). Then, I attempted to access the array. Normally, you would given the index directly. For this example, though, I've made it easier. When accessing an array, you're require to use the sub-script operator ([...]), coupled with an index. The 4 in this example is the element I want to access. I took 1 away from it to get the index of the 4th element. C++ will then return the 4th element of the array. There's two other ways to access an array, but I want state them here.

Pointers


A pointer is a variable, but a bit different from your average int X variable. A pointer points to another previously defined variable. With that said, it's safe to say that the value of a pointer is the address of another variable.

A pointer takes this form: <Type> *<Name>. Here, <Type> is the type of variable the pointer will point to. <Name> is obviously the name of the pointer.

C++ is quite strict with pointers and the objects they point to. When assigning a pointer to a variable, you have to explicitly pass it the variable's address. An address is the location in memory where the variable resides. This is usually represented in hexadecimal. Here's an example:

1
2
int My_variable(0);
int *My_pointer(&My_variable);

In this code, I first declared a basic int variable. I then created a pointer. To initialised my pointer, I had to give the pointer an address to point to. I obtained the address with the address-of operator. This operator takes the form of the ampersand (&). The address-of operator is an unary operator (an operator that takes only 1 operand). Its job it to return the address of its operand. When the pointer is given this address, it points to it.

Note the funky use of parentheses. This is called functional notation. It's equivalent to:

1
2
int My_variable = 0;
int *My_pointer = &My_variable;

Now, pointers aren't treated the same way as variables. Instead, they require dereferencing with the asterisk (*). This is the term used to ask the pointer what it's pointing to. Dereferencing can be worded as: "Pointer, what are you pointing to?". A dereferencing operation returns the object it points to. Still using the code above:

 
*My_pointer = 10;

This assignment statement does 2 things:

1) The compiler sees the asterisk and realises that you want the object pointed-to by My_pointer. This operations returns My_variable because My_pointer was set to point to it initially.

2) With point 1 in mind, the compiler assigns My_variable the value of 10. Don't forget that My_pointer is pointing to My_variable.

This is the best I can do while in my current condition -- I'm kinda drunk, you know, from a night out.

Wazzak
Last edited on
Ok cool I'll look over those articles. In the mean time I have another question. My teacher talks about using arrays with max and min type functions. For example, a user is suppose to select a max height and a min height, and then the same for a base so you can calculate the area. So can I use arrays to store the max and min values as a function or am I thinking about that the wrong way.

For example:

1
2
3
4
5
6
int array[2];
int max;
int min;
array[2]={min,max};
printf("Enter min and max value for height");
scanf("%i %i,&min,&max); 


Would something like this work? If not what am I doing wrong?
return 0;
you dont need return 0 i thought???
Ok so it would work as long as I had a return command?
every function returns a value unless it is declared 'void'. Example:

1
2
3
4
5
6
int hello()
{
     cout<< "hi"<< endl;
     waituser();
     return 0;
}


1
2
3
4
5
6
7
8
9
10
void waituser()
{
    cout<< endl<< endl<< endl<< endl<< endl<< endl<< endl<< endl;
    cout<< "Press anything to continue"<< endl;
    _cl();
    while(!_kbhit())
    {
        continue;
    }
}


if the function is declared as a string, it returns a string. if it declared an integer, it returns and integer. If it is declared a character, it returns a character, and ect... ect...

Because all waituser() is used for is to prompt the user whether he/she wants to continue, and not for any data calculations, we can declare it as void. If we want to create a function that gets the character of a key the user has pressed, the function should return a character. Example:

1
2
3
4
5
6
7
char get_key_press()
{
    char ch;
    while(_kbhit()) _getch();   //clears the _getch() stream
    ch = _getch();
    return ch;
}


or if we want to get the line of a file and return it as a string:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
string get_line_of_file(string filename)
{
    string line;
    ifstream f;
    f.open(filename.c_str(), ios::in);
    if(!f.is_open())
    {
       f.close();
       return "error";
     }
    getline(f, line);
    f.close();
    return line;
}


so, you get the point. I hope this helps!
Last edited on
Topic archived. No new replies allowed.