Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8796819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:42:35+00:00 2026-06-13T23:42:35+00:00

What I have: char cmd[50] = some text here; char v[] = {‘a’,’s’,’d’,’c’,’b’}; So

  • 0

What I have:

char cmd[50] = "some text here";
char v[] = {'a','s','d','c','b'};

So I want to concatenate cmd by adding a letter from v.

Obviously:

strcat(cmd, v[3]);

doesn’t work because strcat doesn’t accept the v[n] parameter n = int.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:42:36+00:00Added an answer on June 13, 2026 at 11:42 pm

    Problems with your approach.

    • C strings must end in 0 byte, in other words '\0' character. Using "" adds that automatically, but otherwise you have to add it yourself, and all string functions depend on that 0 being there.

    • Your v array contains characters, not strings, and strcat takes strings.

    One solution:

    char cmd[50] = "some text here";
    char *v[] = {"a","s","d","c","b"};
    strcat(cmd,v[3]);
    

    This turns your char array into array of pointers to C strings.

    Also, it’s your responsibility to take care that, cmd[] contains enough space to hold whatever you add to it with strcat (here it does). It’s usually best to use snprintf to do string concatenation, as it takes total size of the target array including terminating null, and adds that null there always, so it’s harder to mess up. Example with your original char array:

    char cmd[50] = "some text here";
    char buf[50];
    char v[] = {'a','s','d','c','b'};
    snprintf(buf, sizeof buf, "%s%c", cmd, v[3]);
    

    Notes: sizeof like this works only when buf really is an array, declared with [] like here. Also with snprintf, using same buffer both as destination and format argument may yield unexpected results, so I added a new destination buffer variable.

    One more snprintf example, with your original two arrays only, appending to end of current contents of cmd:

    snprintf(cmd + strlen(cmd), (sizeof cmd) - strlen(cmd), "%c", v[3]);
    

    So clearly, in this particular case, the strncat(cmd, &v[3], 1) suggested in other answers to add 1 character is much nicer, but benefit of snprintf is, you can add all datatype supported by printf, not chars.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an example here: int runcmd(char *cmd) { char* argv[MAX_ARGS]; pid_t child_pid; int
I have a char* like this 1 10 14 16 I want to split
I have a char type column in table. I want to convert it to
I have a char type array[100] with 100 bytes stored in it. I want
Basically I have a script in Python that grabs the text from an open
I have this static std::string exec(char* cmd) { FILE* pipe = _popen(cmd, r); if
I have a 'search' textbox. When a user enters a text, I want to
I have following statement and it compiles: static unsigned char CMD[5] = {0x10,0x03,0x04,0x05,0x06}; int
If we have char *val = someString; and then say if(val){ .... } what
Assuming I have char C whose ascii code is 0110 0111 . How can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.