CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Feb 2013
    Posts
    21

    error C2440: '=' : cannot convert from 'const char *' to 'char *

    Hi,
    I want to do following , but it gives error C2440: '=' : cannot convert from 'const char *' to 'char *.

    ............
    string str("VRML");
    char *sss[];
    sss[0]=str.c_str();
    cout<<"SSS:"<<sss[0]<<endl;
    ............

    Is there anybody who have experienced this problem?

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: error C2440: '=' : cannot convert from 'const char *' to 'char *

    Quote Originally Posted by nihad View Post
    Hi,
    I want to do following , but it gives error C2440: '=' : cannot convert from 'const char *' to 'char *.
    Code:
    string str("VRML");
    char *sss[];
    sss[0]=str.c_str();
    cout<<"SSS:"<<sss[0]<<endl;
    Is there anybody who have experienced this problem?
    Before fixing the "convert" error you first have to fix your other problem: you are trying to access the first element of the char *sss[] array while this array is empty (there is not any element at all!)

    BTW, what are you trying to achieve with this code?
    Victor Nijegorodov

  3. #3
    Join Date
    Feb 2013
    Posts
    21

    Re: error C2440: '=' : cannot convert from 'const char *' to 'char *

    Actually this is my code: I use command line argument, argv[1] is my one of the input file name. I want to change argv[2] and argv[3] dynamically inside the for loop.

    int main(int argc, char *argv[])
    {
    mf::Mesh mesh;
    mf::Reader reader;
    mf::Writer writer;
    mf::Normalizer normalizer;
    int n;

    if (argc != 2) {
    std::cerr << "usage: " << argv[0] << " [Input OBJ Path] " << std::endl;
    exit(EXIT_FAILURE);
    }


    /////// New Code Start/////////////
    FILE *fp;

    fp=fopen(argv[1],"r");

    unsigned int i,j;
    unsigned int number_of_lines = 0;
    unsigned int count_row=0;

    double *read_test_off_file;

    std::string line;
    std::ifstream myfile(argv[1]);

    while (std::getline(myfile,line))
    {
    ++number_of_lines;
    }

    std::cout << "Number of lines in text file: " << number_of_lines<<endl;

    read_test_off_file=new double[number_of_lines];

    for(i=0;i<number_of_lines;i++)
    {
    fscanf(fp,"%lf",&read_test_off_file[i]);
    }

    int test=0;
    cout<<"Scanf Complete."<<endl;
    for(j= 0;j<number_of_lines;j++)
    {
    cout<< j << ":" << read_test_off_file[j] << endl;

    std::string first_part("m");
    std::string second_part;
    std:stringstream ss;
    ss << read_test_off_file[j];
    second_part = ss.str();
    std::string normalize_part("_normalize");
    std::string last_part(".off");

    std::string input_file_name=first_part+second_part+last_part;
    argv[2]=input_file_name.c_str();

    std::string output_file_name=first_part+second_part+normalize_part+last_part;

    argv[3]= output_file_name.c_str();

    if (reader.read_off(mesh, argv[2]) == false)
    {
    exit(EXIT_FAILURE);
    }



    normalizer.triangulation(mesh);
    normalizer.translation(mesh);
    normalizer.scaling(mesh);
    normalizer.rotation_point_svd(mesh, 16500);

    writer.write_off(mesh, argv[3]);

    test++;

    }

    ....................................................

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: error C2440: '=' : cannot convert from 'const char *' to 'char *

    Quote Originally Posted by nihad View Post
    Actually this is my code: I use command line argument, argv[1] is my one of the input file name. I want to change argv[2] and argv[3] dynamically inside the for loop.
    First, use code tags when posting code. The code you posted is practically unreadable.

    Second, you should not be attempting to change argv[] directly. Just copy the values of argv[] to your own local string variables and manipulate those local strings. There is no guarantee that argv[] is modifiable, and even they are modifiable by chance, modifying them is undefined behaviour.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Feb 2013
    Posts
    21

    Re: error C2440: '=' : cannot convert from 'const char *' to 'char *

    Finally, I did it. It will be writer.write_off(mesh, output_file_name.c_str()); instead of argv[3]= output_file_name.c_str();

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: error C2440: '=' : cannot convert from 'const char *' to 'char *

    Quote Originally Posted by nihad View Post
    Finally, I did it. It will be writer.write_off(mesh, output_file_name.c_str()); instead of argv[3]= output_file_name.c_str();
    And what about this erroneous line?
    Code:
    argv[2]=input_file_name.c_str();
    Did you change that to not modify argv[2]?

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Feb 2013
    Posts
    21

    Re: error C2440: '=' : cannot convert from 'const char *' to 'char *

    Yes. It would be reader.read_off(mesh, input_file_name.c_str()) instead of argv[2]=input_file_name.c_str().

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: error C2440: '=' : cannot convert from 'const char *' to 'char *

    Quote Originally Posted by nihad View Post
    Yes. It would be reader.read_off(mesh, input_file_name.c_str()) instead of argv[2]=input_file_name.c_str().
    Also, your code has a memory leak:
    Code:
    read_test_off_file=new double[number_of_lines];
    Where is the delete[] for this call to new[]?

    But in general, coding like this in this day and age of C++ is not necessary. You have CArray if using MFC, and std::vector<> for dynamic arrays, removing the need for new[]/delete[].
    Code:
    #include <vector>
    //..
    std::vector<double> read_test_off_file;	
    //...
    read_test_off_file.resize(number_of_lines);
    //...
    Then when you need to pass a double*, just pass the address of the first element of the vector.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; February 3rd, 2013 at 10:01 AM.

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: error C2440: '=' : cannot convert from 'const char *' to 'char *

    More issues:
    Code:
    FILE *fp;
    
    fp=fopen(argv[1],"r");
    You never checked to see if fp is NULL. What if that file doesn't exist?

    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Feb 2013
    Posts
    21

    Re: error C2440: '=' : cannot convert from 'const char *' to 'char *

    Ya, I should include some code for checking.

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: error C2440: '=' : cannot convert from 'const char *' to 'char *

    Quote Originally Posted by nihad View Post
    Actually this is my code: I use command line argument, argv[1] is my one of the input file name. I want to change argv[2] and argv[3] dynamically inside the for loop.
    Why change argv[2] and argv[3]?
    Don't ever try!
    Instead save their values in string variables (or in a vector of string objects) and only after this change them dynamically!
    Victor Nijegorodov

  12. #12
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: error C2440: '=' : cannot convert from 'const char *' to 'char *

    Quote Originally Posted by nihad View Post
    Hi,
    I want to do following , but it gives error C2440: '=' : cannot convert from 'const char *' to 'char *.

    ............
    string str("VRML");
    char *sss[];
    sss[0]=str.c_str();
    cout<<"SSS:"<<sss[0]<<endl;
    ............

    Is there anybody who have experienced this problem?
    First of all, this code gives two errors, not one. And the omitted error is much more interesting.
    Code:
    D:\Temp\77>cl 77.cpp /EHsc
    Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    77.cpp
    77.cpp(9) : error C2133: 'sss' : unknown size
    77.cpp(10) : error C2440: '=' : cannot convert from 'const char *' to 'char *'
            Conversion loses qualifiers
    Here you need to understand what is the difference between the following definitions in C/C++:
    Code:
    char* sss0[ ]; // this just defines a pointer to an array of char* of unknown size, results in no array allocation!
    char* sss1[1]; // this defines an array of char* of size 1, results in array allocation
    Second error means that pointer to non-constant string is assigned with address pointing to constant string. Using non-constant pointer to constant memory may result in constant object being modified, which conflicts with meaning of being constant. No such conversion exists that would keep the referred object be constant. Therefore you need to explicitly make a modifiable object copy instead of casting pointers. Or make the lvalue be of the type compatible with the type of rvalue.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        string str("VRML");
        const char *sss[1];
        sss[0]=str.c_str();
        cout<<"SSS:"<<sss[0]<<endl;
    
        return 0;
    }
    Code:
    D:\Temp\77>cl 77.cpp /EHsc
    Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    77.cpp
    Microsoft (R) Incremental Linker Version 10.00.40219.01
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    /out:77.exe
    77.obj
    Code:
    D:\Temp\77>77.exe
    SSS:VRML
    Last edited by Igor Vartanov; February 3rd, 2013 at 02:22 PM.
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured