3

I have the below program written in C++:

#include <iostream>

using namespace std;

int main()

{
    int age[5];
    char name[5][10];

    age[0]=10;
    age[1]=20;
    age[2]=30;
    age[3]=25;
    age[4]=40;

    name[0]="abc";
    name[1]="abc";
    name[2]="abc";
    name[3]="abc";
    name[4]="abc";

    cout<<name[0]<<" is "<<age[0]<<"years old";
    cout<<"\n";
    cout<<name[1]<<" is "<<age[1]<<"years old";
    cout<<"\n";
    cout<<name[2]<<" is "<<age[2]<<"years old";
    cout<<"\n";
    cout<<name[3]<<" is "<<age[3]<<"years old";
    cout<<"\n";
    cout<<name[4]<<" is "<<age[4]<<"years old";
    cout<<"\n\n";

    system("PAUSE");

}

When I compile and run it, I get these errors:

error C2440: '=' : cannot convert from 'const char [3]' to 'char [10]' There is no context in which this conversion is possible

error C2440: '=' : cannot convert from 'const char [2]' to 'char [10]' There is no context in which this conversion is possible

error C2440: '=' : cannot convert from 'const char [2]' to 'char [10]' There is no context in which this conversion is possible

error C2440: '=' : cannot convert from 'const char [2]' to 'char [10]' There is no context in which this conversion is possible

error C2440: '=' : cannot convert from 'const char [2]' to 'char [10]' There is no context in which this conversion is possible

I am running MSVC 2008 under Windows 7. I have tried many possible solutions but I failed in fixing this. Any help would be appreciated,

4 Answers 4

3

You are treating the name array as if it was defined thus:

char *name[5];

So either define it that way, or use the following code to populate it:

strcpy(name[0], "abc");
strcpy(name[1], "abc");
strcpy(name[2], "abc");
strcpy(name[3], "abc");
strcpy(name[4], "abc");

I prefer the former choice. The point being you are trying to assign a char * to a char [] which is what strcpy is for. Given you are manipulating initialized C strings in this case anyway, you might as well deal with char * throughout the code.

Sign up to request clarification or add additional context in comments.

Comments

2

You should use std::string for this purpose. The use of char* and char[] to represent strings is deprecated in C++ for many good reasons.

Comments

0

Given the program snippet, name can be initialized at the declaration itself.

char name[5][10] = { "abc", "abc", "abc", "abc", "abc" } ;
       // ^ index 5 is not necessary. char name[][10] = { .. } would also suffice.   

Specified the length of each row is 10 but only using first 3 indexes of it. Every 3rd index ( i.e., 4th element in the array ) is automatically added with a '\0'.

Initialization can be done in case of age array too.

Comments

0

You can use also std::string name[10] instead of 2d char's array. In this case only you can assign new values to the strings through operator '='. Otherwise you should to use array of char* and use strcpy() function for assignment.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.