0

I'm having difficulty appending a character value of an array onto a string (handSorted). The hand[] is a predefined array of text.

char *handSorted = malloc(strlen(hand)+1);
strcat(handSorted, hand[2]);

For example I would like handSorted to be a string of the value of hand[2], the letter 'A'.

3
  • What language are you coding in? Commented Sep 29, 2015 at 23:23
  • 1
    In C, sorry added that to the tag now. Commented Sep 29, 2015 at 23:24
  • You can use a for loop, you can use memset, you can use strdup and many other/ Commented Sep 29, 2015 at 23:29

2 Answers 2

3

When dealing with C, it is good to learn how to use the manual pages in the terminal. Here is the entry for strcat.

DESCRIPTION
 The strcat() and strncat() functions append a copy of the 
 null-terminated string s2 to the end of the null-terminated
 string s1, then add a terminating `\0'.  

That's one problem. You need handSorted to be null terminated.

char *handSorted = malloc(strlen(hand)+1);
handSorted[0] = '\0';
strcat(handSorted, hand[2]);

But there is still a problem. hand[2] is a single character, and strcat() expects a character pointer, AKA a string. So you need to pass it the address of a character using the 'address-of' operator - the &. Like this.

char *handSorted = malloc(strlen(hand)+1);
handSorted[0] = '\0';
strcat(handSorted, &hand[2]);

I think that is what you we're after.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you so so much, I had the null termination already, forgot to add stupidly, but the character pointer helped me so much! Thank you!
Dai, can you explain a bit more? This code snippet I have provided is very short and without more information it is hard to know if my snippet will work in all cases. But I don't see why hand[3] != '\0' would cause it to fail.
As I understand the given variables, handSorted and hand are both type of char*, now with this at mind, assuming handSorted[0] == '\0', strcat would perform the same as hand.substr(2, hand.length - 1). See for yourself
Not quite. Your example is exactly what I expected, and what should occur given my solution. Rereading the original question now I see what Dai meant with his comment. The original question just wanted to store a single character into handSorted. But since he was using char* and strcat I made the (incorrect) assumption that he wanted to be working with strings.
0

The strcat function expects both arguments to be null-terminated strings (pointers, too), as you're passing into a character by value this is going to lead to undefined behavior (probably a segfault crash as it would try to read a low memory address likely outside an allocated area).

If you're adding a single character, you might as well just set the character value directly:

size_t charLength = strlen( handSorted );
assert( charLength < sizeof( handSorted ) + 1 ); // assuming handSorted hasn't decomposed from char[N] to char*.
handSorted[ charLength     ] = hand[2]; // overwrite existing null-terminator with desired char
handSorted[ charLength + 1 ] = '\0'; // set new null-terminator

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.