LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Why double standard? (https://www.linuxquestions.org/questions/programming-9/why-double-standard-436763/)

Dstruct0 04-19-2006 10:32 AM

Why double standard?
 
I'm trying learn gnu c++. And already found a problem. Why the following first code snippet complains but the other doesn't?

Code:

  int **aa = new int*[3];

  aa[0] = new int[2];

  *(*(aa + 0) + 0) = 1;

  cout << (*aa[0])[0] << endl; //complains about: error: invalid types `int[int]' for                           
                                //array subscript

--------------------------------------------------------------------------------------------------------------


  string *first_names[100];

  string first = "LLLL";

  first_names[0] = new string(first);

  cout << (*first_names[0])[0] << endl; //doesn't complain

I can access one array with this notation but not the other? Can anyone kindly explain?

Thanks.

addy86 04-19-2006 10:47 AM

aa[0] is of type int*
*aa[0] is of type int (and equivalent to aa[0][0])
firstnames[0] is of type string*
*firstnames[0] is of type string (and equivalent to firstnames[0][0])

std::string overloads operator[], int doesn't; that's why (*aa[0])[0] will give you an error.
You probably meant aa[0][0] (and firstnames[0][0]).

Dstruct0 04-19-2006 10:56 AM

Thank you for clearing this up for me. I'm enjoyin' every moment of c++. Again thanks.


All times are GMT -5. The time now is 02:10 AM.