Jump to content
  • Advertisement
Sign in to follow this  
JasonL220

Char array joining

This topic is 4218 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
strcat() will append one string to another, but be careful you have enough space in the destination buffer.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
What kind of program could you be writing for a PIC that requires you to concatenate strings, anyway?

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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';

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!