Need help with ARRAY

Can somebody explain the logic that why am I having following output for the typed code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

using namespace std;

int main ()
{
   
    char *bookstr[] = {"Math", "Physics", "Chemistry", "DSP"};

    for (int loop = 0; loop <= 3; loop++)
        cout << loop+1 << "." << *bookstr[loop] << "\n";

    return 0;
}



1.M
2.P
3.C
4.D
If I were you I would make line 8
const char *bookstr[] = {"Math", "Physics", "Chemistry", "DSP"}

and I would remove the * in line 11:
cout << loop+1 << "." << bookstr[loop] << "\n";

Actually, I would probably stop using c-strings and go over to std::string.
Last edited on
bookstr is an array of char-pointers.

So bookstr[loop] is one char-pointer.

So *bookstr[loop] is what one char-pointer is pointing at.

What kind of object does a char-pointer point at? A char.

So what are you sending to cout? A char.

So what appears on screen? A char.
@lastchance
Thanks for the advise dear.
@ Repeater

Thanks dear..that is exactly the answer I was looking for.
Topic archived. No new replies allowed.