what is the [i] mean in this code?

I was trying to finish a programming exercise, but i gave up and used the answer. but what does the [i] mean in the arrays in this code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <cctype>
#include <string>
 
struct bop {
char fullname[40]; // real name
char title[20]; // job title
char bopname[40]; // secret BOP name
int preference; // 0 = fullname, 1 = title, 2 = bopname
};
 
int main()
{
 
using namespace std;
bop Programmer[5] = {
"Alex Ignatkov","System Analyst","IGNAT",2,
"Bob Emerson","Manager","BOBZ",1,
"John Doe","Programmer","DOE",0,
"Bob Evans","Debugger","EVS",2,
"Nick Johns","Tester","NJ",1};
 
char choice;
 
cout << "a. display by name       b. display by title\n";
cout << "c. display by bopname        d. display by preference\nq. quit\n";
 
do
{
    cout << "\nEnter your choice: ";
    cin >> choice;
    choice = tolower(choice);
 
    if (choice != 'a' && choice != 'b' && choice !='c' && choice !='d')
        continue;
    else for (int i=0; i<5; i++)
    {
        switch (choice)
        {
            case 'a': cout << "\n" << Programmer [i].fullname;break;
            case 'b': cout << "\n" << Programmer [i].title;break;
            case 'c': cout << "\n" << Programmer [i].bopname;break;
            case 'd': switch (Programmer[i].preference)
                      {
                        case 0: cout << "\n" << Programmer [i].fullname;break;
                        case 1: cout << "\n" << Programmer [i].title;break;
                        case 2: cout << "\n" << Programmer [i].bopname;break;
                        }
        }
    }
 
} while (choice != 'q');
 
cout << "\nBye!\n";
 
cin.get();
cin.get();
return 0;
}
closed account (o3hC5Di1)
Hi there,

In this piece of code:
for (int i=0; i<5; i++) // line36
i is defined.

During this for loop, i functions as a counter which starts at value 0 and increments by one until it is 4 (smaller than 5).

Thus every iteration i will be a higher number allowing you to traverse or loop through an array's elements.
The first iteration Programmer[0] will be used, then Programmer[1] and so on and so forth.

Hope that helps.

All the best,
NwN
if you mean "i" then its a counter

if you mean "Programmer [i]" then its (i+1)th programmer's data name.

Hope you understand in short.If not then let me know.I can write more about it.
Specifically to this case, i is used to represent the index of the array, or "i". Since it was so common to see this, most programmers just use i in their for loops, but technically, any variable name works. In more complex programs, I change the name of i to something more descriptive, like menuOption, or something that explains what in my array I'm looking for.
closed account (zb0S216C)
It's called the sub-script operator. It's used to obtain an element from an array. The expression that sits between the opening and closing bracket ([...]) must evaluate at an integral value. This integral value is called an index. The index for the first element is zero, and the index for the last is N - 1, where N is the array length.

Technical Extension:
The sub-script operator performs 2 secret operations:

- Dereference
- Addition

The address of the first element is offset by the index specified within the sub-script operator. The resulting address is dereferenced; thus yielding the element at the specified index. In other words, the sub-script operator is equal to this:

 
*(array_ + index_);

The parentheses here are necessary, because the asterisk (*) has a higher precedence than addition.

Wazzak
Last edited on
Where do you learn this kind of stuff? I understand why it's relevant in some cases, but this isn't something I can just recite off of the top of my head (I also believe this is a little more of an explanation than what the OP had asked for). Thanks for sharing though.
closed account (zb0S216C)
I learn from technical blogs, Stack Overflow, FAQs, this site, etcetera.

Volatile Pulse wrote:
"I also believe this is a little more of an explanation than what the OP had asked for"

Hence the "Technical Extension" header.

Wazzak
Last edited on
Just lots of reading and working with it and becomes second nature. I think Moschops has an article about pointers that discusses the concept Framework just showed.
closed account (o3hC5Di1)
Just wanted to drop a quick "Thanks!" to Framework - very informative as always.

All the best,
NwN
Topic archived. No new replies allowed.