-1

Usually, I access an array in C++ by the syntax foo[2], where 2 is the index of an array.

In the below code. I didn't understand how this code is giving output and access this array by index 'b', 'c'. I am confused it is array index or something else.

int count[256] = {0};
count['b'] = 2;
cout << count['b'] << endl; //output 2
cout << count['c'] << endl; //output 0

Output

2
0
Melebius
  • 6,183
  • 4
  • 39
  • 52
compsy
  • 233
  • 4
  • 12
  • 2
    A `char` is an `int` is an `int`. – CinCout Dec 14 '17 at 11:48
  • 1
    You want `std::map`? – iBug Dec 14 '17 at 11:49
  • 1
    Computers cannot store characters; they store characters by encoding them into numbers. The character 'b' evaluates to integer 98. Hence, you end up accessing `count[98]` when you do `count['b']`. – Yashas Dec 14 '17 at 11:52
  • @CinCout I didn't get it. Can you give any reference or example – compsy Dec 14 '17 at 11:53
  • @Yashas `'b'` is not always 98. This depends on encoding. – iBug Dec 14 '17 at 11:53
  • AFAIK, ASCII characters have an equal encoding in UTF too. – Yashas Dec 14 '17 at 11:54
  • @Yashas. Initlize array of size 90 **int count[90] = {0};** then why it is giving output **cout << count['b'] << endl;** means **count[98]** – compsy Dec 14 '17 at 11:55
  • You are going out of bounds. It is undefined behavior. As long as the OOB access remains within the program's memory, you will have UB but your program will continue running. If you try to access another program's memory, you will get a segmentation fault. Try writing `count[98]` directly and it might still work. – Yashas Dec 14 '17 at 11:57
  • @Yashas The array is defined as `int count[256]`. Where is OP going out of bounds? – Melebius Dec 14 '17 at 12:04
  • 1
    @Melebius The author mentioned in their comment that he changed the size to 90. My comment was a reply to their comment not to the post. Forgot to tag :/ – Yashas Dec 14 '17 at 12:04

3 Answers3

2

Type char is actually an integral type. Every char value represented by a character literal has an underlying integral value it corresponds to in a given code page, which is probably an ASCII table. When you do:

count['b'] = 2;

you actually do:

count[98] = 2;

as character 'b' corresponds to an integral value of 98, character 'c' corresponds to an integral value of 99 and so on. To illustrate, the following statement:

char c = 'b';

is equivalent of:

char c = 98;

Here c has the same underlying value, it's the representation that differs.

Ron
  • 14,674
  • 4
  • 34
  • 47
  • I Initlize array of size 90 **int count[90] = {0};** then why it is giving output cout << count['b'] << endl; means count[98] . Why it is not giving error as iI think **out of index** – compsy Dec 14 '17 at 12:02
  • 1
    You initialize an array of 256 elements. If you did initialize your array with 90 elements then reading out of bounds would invoke undefined behavior. – Ron Dec 14 '17 at 12:04
  • 1
    @compsy https://stackoverflow.com/questions/9137157/no-out-of-bounds-error – Yashas Dec 14 '17 at 12:06
2

Remember that in c++ characters are represented as numbers. Take a look at this ascii table. http://www.asciitable.com

According to this the character 'b' is represented 98 and 'c' as 99. Therefore what your program is really saying is...

int count[256] = {0};
count[98] = 2;
cout << count[98] << endl; //output 2
cout << count[99] << endl; //output 0

Also incase you don't know saying an array = {0} means zero initialize every value so that is why count['c'] = 0.

In C/C++ there is not 8 bit / 1 byte integer. We simply use the char type to represent a single (signed or unsigned) byte and you can even put signed and unsigned infront of the char type. Char really is just another int type which we happen to use to express characters. You can also do the following.

char b = 98;
char c = 99;
char diff = c - b; //diff is now 1
chasep255
  • 11,745
  • 8
  • 58
  • 115
  • _“We simply use the char type to represent the signed byte”_ It is not defined by the standard, whether the plain `char` is signed or unsigned. You should use `signed char` if you want a signed 8-bit number. – Melebius Dec 14 '17 at 11:59
0

Because characters are always represented by integers in the computer, it can be used as array indices.

You can verify by this:

char ch = 'b';
count[ch] = 2;
int i = ch;
cout << i << endl;
cout << count[i] << endl;

Usually the output is 98 2, but the first number may vary depending on the encoding of your environment.

iBug
  • 35,554
  • 7
  • 89
  • 134