Char array joining

Started by
19 comments, last by Zahlman 17 years, 1 month ago
Hi, i cant find or remember how to join to char arrays, eg char first[] = "john"; char last[] = "smith"; char name[] = first + last; is it done like that? i'm programming a pic microcontroller so C++ string are out of the question.
Advertisement
Quote:Original post by JasonL220
char first[] = "john";
char last[] = "smith";

char name[] = first + last;


is it done like that?


No, not even close... where did you come up with that? does that even compile on your compiler?

Quote:
i'm programming a pic microcontroller so C++ string are out of the question.


...why? have you tried using them? what is the trouble? and what standard library implementation / compiler are you using?
strcat() will append one string to another, but be careful you have enough space in the destination buffer.
Quote:Original post by rip-off
strcat() will append one string to another, but be careful you have enough space in the destination buffer.

How about strncat? I guess buffer exploits aren't much of an issue on a PIC microcontroller, but it could help you avoid stack corruption and there's no harm in forming good habits.

Admiral
Ring3 Circus - Diary of a programmer, journal of a hacker.
What kind of program could you be writing for a PIC that requires you to concatenate strings, anyway?
strncat( name, first, sizeof(name)-1 );strncat( name, last, sizeof(name)-1 );


Thats if you have a strncat function. If not, then strcat, which is dangerous.

strcat( name, first );strcat( name, last );


If not, then you have to define it yourself.
char * strcat( char * to, const char * from ){  char * mid = to;  while (*mid) mid++;  while (*from) *(mid++)=*(from++);  return to;}
william bubel
Quote:Original post by Inmate2993
strncat( name, first, sizeof(name)-1 );strncat( name, last, sizeof(name)-1 );


The correct version would need to create a correctly sized string buffer to store the entire concatenated strings, use strncpy for the first (unless you set the first character to nul) and add the nul terminator at the end in case you didn't manage to get the length right.:

int len = strlen(first) + strlen(last);char *name = malloc(len + 1);strncpy(name, first, len);strncat(name, last, len);name[len] = '\0'; 
This little function I knocked up the other day will do the trick ...

Despite it being potentially unsafe, I'm quite pleased with the linear cost when compared to 'strcpy' then 'strcat' and find it quite useful when assembling filenames, etc.

char* strcpycat( char* dest, const char* src1, const char* src2 ){  while ( *src1 ) *dest++ = *src1++;  while ( *src2 ) *dest++ = *src2++;  *dest = NULL;  return dest;}


-----

char first[] = "john";
char last[] = "smith";

char name[ 100 ];
strcpycat( name, first, last );


>> 'name' is now: 'johnsmith\0'

----

NOTE, obviously you'll need to ensure that the 'dest' buffer is large enough (and that both source strings are null terminated, which they are in your example)

Hope this helps.
Quote:
*dest = NULL;


What the? I think someone is mixing up NULL (the invalid pointer) and NUL (the terminator character). The literal you're looking for is '\0'.

Your suggestion of avoiding unnecessary traversal stands (even though an strcpy + strcat approach is still linear, and you still have to do those strlen anyway). If I were to handle it as a clean function, I'd probably write:

char *concat(char *a, char *b){  int la, lb;  char *dest;   assert (a);  assert (b);  la = strlen(a);  lb = strlen(b);  dest = malloc(la + lb + 1);  strncpy(dest,a,la);  strncat(dest+a,b,lb);  dest[la+lb] = '\0';  return dest;}


This is, I believe, the safest you can get in C. In the end, I'd rather spend time getting std::string to work, than rewriting it myself in C...
'\0' and NULL are identical here

This topic is closed to new replies.

Advertisement