Basic char */const char* problem

This is a discussion on Basic char */const char* problem within the C++ Programming forums, part of the General Programming Boards category; I have a problem with some code MyClass.h Code: public: //stuff protected: // stuff char * path; //more stuff void ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    6

    Basic char */const char* problem

    I have a problem with some code
    MyClass.h
    Code:
    public:
      //stuff
    
    protected:
      // stuff
      char * path;
      //more stuff
    
       void function1();
       void function2();
    
    };
    MyClass.cc
    Code:
    //stuff
    
    void MyClass::function1() {
    
          int spinvalue = 50;
          std::cout << "Parameter 1 = " << spinvalue << std::endl;
          std::cout << path << std::endl;
          //stuff
    }
    
    
    void MyClass::function2() 
    {
      //stuff
    
      path = const_cast<char*> ( dialog.get_filename().c_str() );
          std::cout << "pathImagen = " << path << std::endl;
    
      //stuff
    }
    
    // more stuff
    }
    I need a char * in my function1(). I tried some possibilities, all envolving creating a new class variable (path).

    I tried for this variable a char * path, char path[] ... and tried to convert the const char * in that, but for most i got a memory crash and for others i was not obtaining the value in function1().

    I don't know what is wrong with it, but when i run function2() and function1() in this order i got :

    1 pathImagen = /home/myhome/pathToImage/image.jpg
    2 Parameter 1 = 50
    3 �6� �

    The 1, is the cout just after the const_cast<char*>, the 2 is the spinbutton working and the 3 is some random garbage :S

    Anyone know what i'm doing wrong or know the way to do what i want? any lead/hint/help will be appreciated.

    Thx in advance for all!
    Last edited by wisuzu; 12-01-2011 at 08:19 AM.

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    It's hard to tell what your problem is from the little code you posted, but maybe this will help:

    Code:
    include <iostream>
    #include <string>
    using namespace std;
    
    class A {
    
    public:
        const char* str;
        
        void fun(const char* tmpstr) {
            str = tmpstr;
            cout << "str is " << str << endl;
        }
    };
    
    
    int main() {
        A myobj;
        string instr, instr2;
        cout << "enter a string: ";
        getline( cin, instr );
        myobj.str = instr.c_str();
        cout << "str is " << myobj.str << endl;
        
        cout << "enter another string: ";
        getline( cin, instr2 );
        myobj.fun(instr2.c_str() );
        return 0;
    }
    Good luck.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You must use either a real const char* (not a cast one, that is an illegal conversion) for this, or else a sufficiently sized char array and strcpy().
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    6
    Edited
    Last edited by wisuzu; 12-01-2011 at 08:57 AM.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    6
    I answer it myself

    the problem was that the memory that the pointer was pointing is free when the function ends, so i use a std::string as the variable in the header and then use path = std::string(dialog.get_filename().c_str()) and path.c_str() to access where i want
    Solved

    Thx to all the leads and hints!

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    You don't need the ".c_str()".
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    If your class still contains a char * (or a const char *), then you're still doing it wrong.

    Having a class member variable POINTING to something outside the class is a sure way to screw up at some point.

    Make str a std::string str and the problem goes away.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Dec 2011
    Posts
    6
    Quote Originally Posted by Salem View Post
    If your class still contains a char * (or a const char *), then you're still doing it wrong.

    Having a class member variable POINTING to something outside the class is a sure way to screw up at some point.

    Make str a std::string str and the problem goes away.
    Yes, as i said finally the solution to the problem was creating a std::string class variable. Thx all the hints anyways!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 08-09-2011, 12:41 PM
  2. Difference between char *str1 & const char *str2
    By Tigers! in forum C Programming
    Replies: 4
    Last Post: 08-06-2009, 04:04 AM
  3. Replies: 1
    Last Post: 11-16-2008, 02:46 PM
  4. invalid conversion from `const char*' to `char'
    By GameGenie in forum C++ Programming
    Replies: 6
    Last Post: 08-01-2005, 05:30 AM
  5. Assigning Const Char*s, Char*s, and Char[]s to wach other
    By Inquirer in forum Linux Programming
    Replies: 1
    Last Post: 04-29-2003, 10:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21