Non lvalue in assignment error

355 views Asked by At

I got this error when I used names to print the strings, but no errors when tempNames is used.

char* names[] = { "JIM",
                  "RAM",
                  "SAM",
                  0  };
int main(int argc, char *argv[])    
{
    char** tempNames = names ;        
    while(*names != 0)
        std::cout << *names++ << std::endl ; 
    return 0;
}

How come *names became an rvalue whereas *tempNames is an lvalue.

1

There are 1 answers

7
Jonathan Mee On

As Bill Lynch explains in the comments you're incrementing type char* [] that's not allowed. (It's easier if you think of it as type int [].) When you assign to a char** then you can increment it.

Or a better C++ iterator based solution C++11:

char* names[] = { "JIM",
                  "RAM",
                  "SAM" };


int main(int argc, char *argv[])
{

    char** tempNames = names ;

    for(auto i = std::begin(names); i != std::end(names); ++i){
        std::cout << *i << std::endl;
    } 

    return 0;
}