PDA

View Full Version : char array and char * array[]



vanduongbk
12th October 2013, 04:13
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

Santosh Reddy
12th October 2013, 07:22
I hope you are looking Qt way of doing it


QList<QChar> Str1 = QList<QChar>() << 'a' << 'b' << 'c';
QList<QChar> Str2 = QList<QChar>() << 'd' << 'e' << 'f' << 'g';
QList<QChar> Str3 = QList<QChar>() << '1' << '2';
QList<QChar> Str4 = QList<QChar>() << '5' << '6' << '7';

QList<QChar> List = Str1 + Str2 + Str3 + Str4;
qDebug() << List; // ('a', 'b', 'c', 'd', 'e', 'f', 'g', '1', '2', '5', '6', '7')

syamcr
13th October 2013, 16:53
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.

ChrisW67
13th October 2013, 23:07
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.

vanduongbk
16th October 2013, 10:01
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];

ChrisW67
16th October 2013, 23:24
This is basic C programming using Microsoft extensions (strcpy_s (http://msdn.microsoft.com/en-us/library/td1esda9%28v=vs.90%29.aspx) and strcat_s (http://msdn.microsoft.com/en-us/library/d45bbxx4%28v=vs.90%29.aspx)) 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.

Radek
17th October 2013, 10:18
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:


char List[400];
int offs = 0;

memcpy(&List[offs],Str1,BytesFromStr1); // can be sizeof(Str1) if the Str1 is full.
offs += BytesFromStr1;

memcpy(&List[offs],Str2,BytesFromStr2);
offs += BytesFromStr2;

memcpy(&List[offs],Str3,BytesFromStr3);
offs += BytesFromStr3;

memcpy(&List[offs],Str4,BytesFromStr4);
offs += BytesFromStr4;

offs now contains the total nmb of bytes in List. If vanduongbk wants a NULL terminated string, then


List[offs] = 0;


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.