1

This may be a silly question, but I'm struggling to solve this problem. I know that to append a char to a string I can do something like this:

char c;
char string[10] = "";
strcat(string, &c);

Now, this works well for char variables, but the problem is that when I try to append a char from an array:

char array[5];
char string[10] = "";
strcat(string, &array[0]); //&array[0] returns the entire array, not just array[0]

Question: How can I append a single char from an array to a string?

3
  • 2
    first snippet doesn't work correctly. Also You can use strncat for one character addition. Commented Nov 6, 2017 at 0:24
  • 1
    strcat requires a null-terminated string for its second argument. Commented Nov 6, 2017 at 0:30
  • The first part doesn't work either. Try this instead stackoverflow.com/a/7853834/597607 Commented Nov 6, 2017 at 0:33

1 Answer 1

3

You can use strncat().Here length is the number of characters you want to append to string

strncat(string, array, length);

For appending single character, use length = 1

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

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.