Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

concatenate char array in C

Tags:

arrays

c

string

I have a a char array:

char* name = "hello"; 

I want to add an extension to that name to make it

hello.txt 

How can I do this?

name += ".txt" won't work

like image 848
user69514 Avatar asked Feb 07 '10 20:02

user69514


People also ask

Can you concatenate chars in C?

In C, the strcat() function is used to concatenate two strings. It concatenates one string (the source) to the end of another string (the destination). The pointer of the source string is appended to the end of the destination string, thus concatenating both strings.

How do I merge char arrays?

The concat() method concatenates (joins) two or more arrays. The concat() method returns a new array, containing the joined arrays.

How do I add characters to a char array?

append(char[] str) method appends the string representation of the char array argument to this sequence. The characters of the array argument are appended, in order, to the contents of this sequence. The length of this sequence increases by the length of the argument.

Can you concat char to string?

You can use String(char[] value) constructor to convert char array to string. This is the recommended way.


2 Answers

Have a look at the strcat function.

In particular, you could try this:

const char* name = "hello"; const char* extension = ".txt";  char* name_with_extension; name_with_extension = malloc(strlen(name)+1+4); /* make space for the new string (should check the return value ...) */ strcpy(name_with_extension, name); /* copy name into the new var */ strcat(name_with_extension, extension); /* add the extension */ 
like image 171
David Underhill Avatar answered Sep 30 '22 09:09

David Underhill


I have a char array:

char* name = "hello"; 

No, you have a character pointer to a string literal. In many usages you could add the const modifier, depending on whether you are more interested in what name points to, or the string value, "hello". You shouldn't attempt to modify the literal ("hello"), because bad things can happen.

The major thing to convey is that C does not have a proper (or first-class) string type. "Strings" are typically arrays of chars (characters) with a terminating null ('\0' or decimal 0) character to signify end of a string, or pointers to arrays of characters.

I would suggest reading Character Arrays, section 1.9 in The C Programming Language (page 28 second edition). I strongly recommend reading this small book ( <300 pages), in order to learn C.

Further to your question, sections 6 - Arrays and Pointers and section 8 - Characters and Strings of the C FAQ might help. Question 6.5, and 8.4 might be good places to start.

I hope that helps you to understand why your excerpt doesn't work. Others have outlined what changes are needed to make it work. Basically you need an char array (an array of characters) big enough to store the entire string with a terminating (ending) '\0' character. Then you can use the standard C library function strcpy (or better yet strncpy) to copy the "Hello" into it, and then you want to concatenate using the standard C library strcat (or better yet strncat) function. You will want to include the string.h header file to declare the functions declarations.

#include <stdio.h> #include <string.h> #include <stdlib.h>  int main( int argc, char *argv[] ) {     char filename[128];     char* name = "hello";     char* extension = ".txt";      if (sizeof(filename) < strlen(name) + 1 ) { /* +1 is for null character */         fprintf(stderr, "Name '%s' is too long\n", name);         return EXIT_FAILURE;     }     strncpy(filename, name, sizeof(filename));      if (sizeof(filename) < (strlen(filename) + strlen(extension) + 1) ) {         fprintf(stderr, "Final size of filename is too long!\n");         return EXIT_FAILURE;     }     strncat(filename, extension, (sizeof(filename) - strlen(filename)) );     printf("Filename is %s\n", filename);      return EXIT_SUCCESS; } 
like image 23
mctylr Avatar answered Sep 30 '22 10:09

mctylr