Thread: help converting a char array to a string object?

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    22

    help converting a char array to a string object?

    seems like there should be a simple way to do this but i can't seem to figure it out.

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Code:
    #include <string>
    
    
    int main (void)
         {
         char lpStr[256] = "Hi there";
         std::string str = lpStr;
         return 0;
         }
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    22
    my problems a bit more complicated than that.
    just thought i was missing something easy.
    hopefuly these snipets will help you to see my problem.

    what i'm tring to do is pass a pointer to an array of strings
    and populate it with an array of char arrays.

    Code:
    void pkrEdit::GetDirs(string *dirs[])
    {
    	for(int i = 0; i < HStruct.num_dirs; i++)
    	{
    		dirs[i] = DStruct[i].path;
    	}
    }
    DStruct.path is part of a struct:

    Code:
    typedef struct PK_DIR_STRUC
    {
    	char path[32];
    	int file_offset;
    	int file_count;
    }
    seems like it should work but my compiler keep throwing the same exeption:
    error C2440: '=' : cannot convert from 'char [32]' to 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > *'
    sorry i wasn't more clear the first time.

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    not extremely clear. but you could try a cast to (char *).
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    22
    Well i figured it out... turned out it was a misplaced new statment elsewhere in the code. thanks for trying though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM