Results 1 to 4 of 4

Thread: string to char*?

  1. #1
    Join Date
    Jul 2006
    Posts
    79
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default string to char*?

    hello everyone,
    this is a rather newbie question, but strings confuse me way too often.

    i have a readFromTextFile() function
    in which i read in a string array lines of text. string lines[6].
    now i want to save each string to a char * variable.
    i guess i will have to do malloc for each char* variable but then?
    should i memcpy? strcpy? or am i completely of course here?
    what should i do?
    thank you for your help.
    nass

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: string to char*?

    Quote Originally Posted by nass View Post
    i guess i will have to do malloc for each char* variable but then?
    should i memcpy? strcpy?
    memcpy() and strcpy() are almost the same, but since you are dealing with strings, you should use strcpy() or strncpy().

    There are also strdup() and strndup() which work like malloc()+strcpy().

  3. #3
    Join Date
    Jul 2006
    Posts
    79
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: string to char*?

    thank you for the quick reply.
    but is it 'legal' in c++ terms to cast

    (char*)&lines[1], in order to use it as source pointer in strcpy?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: string to char*?

    Quote Originally Posted by nass View Post
    but is it 'legal' in c++ terms to cast

    (char*)&lines[1], in order to use it as source pointer in strcpy?
    No, you have to use string::c_str() to get a const char *.

    http://www.cppreference.com/cppstring/c_str.html

Similar Threads

  1. String value
    By Gayathri in forum Newbie
    Replies: 2
    Last Post: 21st November 2006, 05:44
  2. Create pixmap image from string
    By Morea in forum Qt Programming
    Replies: 5
    Last Post: 17th November 2006, 16:38
  3. How to search a string in xml file with Qt Dom classes
    By doganay44 in forum Qt Programming
    Replies: 2
    Last Post: 5th October 2006, 20:16
  4. inserting string > 127 characters to MS Access
    By jh in forum Qt Programming
    Replies: 0
    Last Post: 12th May 2006, 17:11
  5. How to get size (length, width ....) of string or char
    By Krishnacins in forum Qt Programming
    Replies: 1
    Last Post: 20th March 2006, 09:55

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.