Results 1 to 7 of 7

Thread: char array and char * array[]

  1. #1
    Join Date
    Jan 2013
    Posts
    44
    Thanks
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default char array and char * array[]

    hello everyone
    i have a question as below
    i have variables:
    char Str1[100]; //Str1 ='a','b','c' //the str length is not similar
    char Str2[100]; //Str2='d','e','f','g'
    char Str3[100]; //Str3='1','2'
    char Str4[100]; //Str4='5','6','7'
    char *List[4]={Str1,Str2,Str3,Str4};

    how i can do List as 'a','b','c','d','e','f,'g','1','2','5','6','7'
    plz help me by a demo code,thank

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: char array and char * array[]

    I hope you are looking Qt way of doing it
    Qt Code:
    1. QList<QChar> Str1 = QList<QChar>() << 'a' << 'b' << 'c';
    2. QList<QChar> Str2 = QList<QChar>() << 'd' << 'e' << 'f' << 'g';
    3. QList<QChar> Str3 = QList<QChar>() << '1' << '2';
    4. QList<QChar> Str4 = QList<QChar>() << '5' << '6' << '7';
    5.  
    6. QList<QChar> List = Str1 + Str2 + Str3 + Str4;
    7. qDebug() << List; // ('a', 'b', 'c', 'd', 'e', 'f', 'g', '1', '2', '5', '6', '7')
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Jul 2013
    Posts
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5

    Default Re: char array and char * array[]

    You need to use strcpy/strcat (or better use strncpy & strncat).

    char List[400];
    strcpy(List, Str1);
    strcat(List, Str2);
    strcat(List, Str3);
    strcat(List, Str4);

    Be aware that List now contains copies of characters from Str1 to Str4. Modifying List does not affect the other arrays.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: char array and char * array[]

    strcpy and strcat are a bad idea if the char arrays do not have a NUL terminating byte. There's no indication that is the case here.
    Without terminating NULs you would have to use strncpy to communicate the number of chars to copy (which means you would have to know that count), and the result gains character (a terminating NUL) so the target buffer is not large enough to hold the result if the four source arrays are full.

  5. #5
    Join Date
    Jan 2013
    Posts
    44
    Thanks
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: char array and char * array[]

    Quote Originally Posted by syamcr View Post
    You need to use strcpy/strcat (or better use strncpy & strncat).

    char List[400];
    strcpy(List, Str1);
    strcat(List, Str2);
    strcat(List, Str3);
    strcat(List, Str4);

    Be aware that List now contains copies of characters from Str1 to Str4. Modifying List does not affect the other arrays.
    hi,you
    i have vars as:
    char tr1[30];
    char tr2[30];
    char tr3[30];
    char tr4[30];
    char Print[100][200];

    then,i write a code as:
    for (int i=0;i<10;i++)
    {
    strcpy_s(Print[i+1],3,tr1);
    strcat_s(Print[i+1],21,tr2);
    strcat_s(Print[i+1],24,tr3);
    strcat_s(Print[i+1],22,tr4);
    qdebug()<<Print[i];
    }

    the compiler warning that buffer is too small,i dont know?
    if i only use to tr2,it's ok but can not to tr4
    please help me?

    qDebug()<<Print[i];

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: char array and char * array[]

    This is basic C programming using Microsoft extensions (strcpy_s and strcat_s) to the C library and nothing at all to do with Qt.

    The second argument to strcpy_s/strcat_s is the size of the target buffer not the number of characters to copy as in strncpy/strncat.

    The first time through the loop i == 0 and the loop contents read:
    • Copy the NUL terminated string from tr1 into the 3 character buffer at Print[1]. This will fail if strlen(tr1) > 2.
    • Copy the NUL terminated string from tr2 onto the end of the string in the 21 character buffer at Print[1]. This will fail if strlen(tr1) + strlen(tr2) > 20.
    • Copy the NUL terminated string from tr3 onto the end of the string in the 24 character buffer at Print[1]. This will fail if strlen(tr1) + strlen(tr2) + strlen(tr3) > 23.
    • Copy the NUL terminated string from tr4 onto the end of the string in the 22 character buffer at Print[1]. This will fail if strlen(tr1) + strlen(tr2) + strlen(tr3) + strlen(tr4) > 21.
    • You print the content of Print[0], which is not affected by any of the the stuff above.

    There are other ways this can fail because you keep changing the supposed target buffer size.
    Last edited by ChrisW67; 16th October 2013 at 23:29.

  7. #7
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: char array and char * array[]

    Naturally, ChrisW67. But if we are solving a basic C question and cannot find a suitable solution, then (supposing not a NULL terminated buffers) the function to use is memcpy(). vanduongbk needs to keep info, where to copy and how much to copy. Something like this:
    Qt Code:
    1. char List[400];
    2. int offs = 0;
    3.  
    4. memcpy(&List[offs],Str1,BytesFromStr1); // can be sizeof(Str1) if the Str1 is full.
    5. offs += BytesFromStr1;
    6.  
    7. memcpy(&List[offs],Str2,BytesFromStr2);
    8. offs += BytesFromStr2;
    9.  
    10. memcpy(&List[offs],Str3,BytesFromStr3);
    11. offs += BytesFromStr3;
    12.  
    13. memcpy(&List[offs],Str4,BytesFromStr4);
    14. offs += BytesFromStr4;
    To copy to clipboard, switch view to plain text mode 
    offs now contains the total nmb of bytes in List. If vanduongbk wants a NULL terminated string, then
    Qt Code:
    1. List[offs] = 0;
    To copy to clipboard, switch view to plain text mode 

    List in the original post is an array of pointers. List[0] is simply Str1, List[1][2] is the third character of Str2. It is not a concatenation.

Similar Threads

  1. Char array[6] to QString and display it
    By arunvv in forum Newbie
    Replies: 11
    Last Post: 12th March 2014, 20:48
  2. char array to qstring
    By vani.pv in forum Newbie
    Replies: 4
    Last Post: 16th August 2012, 11:04
  3. Char array to QString
    By gab74 in forum Newbie
    Replies: 11
    Last Post: 13th December 2011, 15:54
  4. Converting QString to char array
    By srohit24 in forum Qt Programming
    Replies: 3
    Last Post: 22nd July 2009, 18:19
  5. Char Array to QString
    By 3nc31 in forum Qt Programming
    Replies: 2
    Last Post: 25th November 2007, 22:18

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
  •  
Qt is a trademark of The Qt Company.