How to copy char array in C?

In C, I have two char arrays:

char array1[18] = "abcdefg"; char array2[18]; 

How to copy the value of array1 to array2 ? Can I just do this: array2 = array1 ?

+64
c arrays char copy
May 20 '13 at 8:38
source share
12 answers

You cannot directly execute array2 = array1 , because in this case you will be manipulating the addresses ( char * ) of the arrays, not their values.

For this situation, it is recommended to use strncpy to avoid buffer overflows , especially if array1 populated from user input (keyboard, network, etc.). Like this:

 // Will copy 18 characters from array1 to array2 strncpy(array2, array1, 18); 

Like @Prof. Falken mentioned in comments, strncpy can be evil . Make sure the destination buffer is large enough to contain the original buffer (including \0 at the end of the line).

+73
May 20 '13 at 8:41
source share

If your arrays are not string arrays, use: memcpy(array2, array1, sizeof(array2));

+22
Apr 24 '14 at 9:03
source share

If you want to protect against lines without interruption, which can cause all kinds of problems, copy your line as follows:

 char array1[18] = {"abcdefg"}; char array2[18]; size_t destination_size = sizeof (array2); strncpy(array2, array1, destination_size); array2[destination_size - 1] = '\0'; 

This last line is really important because strncpy() does not always null terminate lines. (If the destination buffer is too small to contain the entire source string, sntrcpy () will not terminate the null string.)

The manpage for strncpy () even states "Warning: if the first n bytes of src do not have a null byte, the line placed in dest will not end with zero."

The reason strncpy () behaves somewhat strangely is because it was actually not originally designed to safely copy strings.

Another way is to use snprintf () as a safe replacement for strcpy ():

 snprintf(array2, destination_size, "%s", array1); 

(Thanks to jxh for the tip.)

+21
May 20 '13 at 8:49
source share

You cannot assign arrays; names are constants that cannot be changed.

You can copy the contents using:

 strcpy(array2, array1); 

Assuming the source is a valid string and that the destination is large enough, as in your example.

+6
May 20 '13 at 8:39
source share

As others noted, strings are copied with strcpy() or its variants. In some cases, you can also use snprintf() .

You can only assign arrays the way you want, as part of the structure assignment:

 typedef struct { char a[18]; } array; array array1 = { "abcdefg" }; array array2; array2 = array1; 

If your arrays are passed to functions, it will be shown that you are allowed to assign them, but this is just an accident of semantics. In C, the array will decay into a pointer type with the address value of the first member of the array, and that pointer will be passed. So, your array parameter in your function is just a pointer. Assignment is just a pointer assignment:

 void foo (char x[10], char y[10]) { x = y; /* pointer assignment! */ puts(x); } 

The array itself remains unchanged after returning from the function.

This semantic "decay to pointer value" value for arrays is the reason that assignment does not work. The value l has the type of an array, but the r-value is the type of a decomposed pointer, so the assignment is performed between incompatible types.

 char array1[18] = "abcdefg"; char array2[18]; array2 = array1; /* fails because array1 becomes a pointer type, but array2 is still an array type */ 

As to why the β€œdecay to pointer” semantics was introduced, this was to ensure compatibility of the source code with the predecessor of C. You can read C language development for details.

+4
May 20, '13 at 8:50
source share

It should look like this:

 void cstringcpy(char *src, char * dest) { while (*src) { *(dest++) = *(src++); } *dest = '\0'; } ..... char src[6] = "Hello"; char dest[6]; cstringcpy(src, dest); 
+3
Jul 29 '15 at 14:36
source share
 array2 = array1; 

not supported in c. To do this, you need to use functions such as strcpy () .

+1
May 20 '13 at 8:42
source share

I recommend using memcpy () to copy data. Also, if we assign a buffer to another as array2 = array1 , both arrays have the same memory, and any change in arrary1 is also rejected in array2. But we use memcpy, both buffers have different arrays. I recommend memcpy () because strcpy and the associated function do not copy the NULL character.

+1
May 20 '13 at 9:17
source share

c functions below only ... C ++ you need to make a char array, then use a lowercase copy and then use tokenizor functions for strings ... C ++ made it a lot harder to do anythng

 #include <iostream> #include <fstream> #include <cstring> #define TRUE 1 #define FALSE 0 typedef int Bool; using namespace std; Bool PalTrueFalse(char str[]); int main(void) { char string[1000], ch; int i = 0; cout<<"Enter a message: "; while((ch = getchar()) != '\n') //grab users input string untill { //Enter is pressed if (!isspace(ch) && !ispunct(ch)) //Cstring functions checking for { //spaces and punctuations of all kinds string[i] = tolower(ch); i++; } } string[i] = '\0'; //hitting null deliminator once users input cout<<"Your string: "<<string<<endl; if(PalTrueFalse(string)) //the string[i] user input is passed after //being cleaned into the null function. cout<<"is a "<<"Palindrome\n"<<endl; else cout<<"Not a palindrome\n"<<endl; return 0; } Bool PalTrueFalse(char str[]) { int left = 0; int right = strlen(str)-1; while (left<right) { if(str[left] != str[right]) //comparing most outer values of string return FALSE; //to inner values. left++; right--; } return TRUE; } 
+1
11 Oct '16 at 20:52
source share

for integer types

 #include <string.h> int array1[10] = {0,1,2,3,4,5,6,7,8,9}; int array2[10]; memcpy(array2,array1,sizeof(array1)); // memcpy("destination","source","size") 
0
Dec 12 '16 at 19:56
source share

You cannot assign arrays to copy them. How to copy the contents of one into another depends on several factors:

For char arrays, if you know that the source array has a zero end, and the target array is large enough for a string in the source array, including the null terminator, use strcpy() :

 #include <string.h> char array1[18] = "abcdefg"; char array2[18]; ... strcpy(array2, array1); 

If you don't know if the destination array is enough, but the source is the string C, and you want the destination to be the correct string C, use snprinf() :

 #include <stdio.h> char array1[] = "a longer string that might not fit"; char array2[18]; ... snprintf(array2, sizeof array2, "%s", array1); 

If the original array is optionally null-terminated, but you know that both arrays are the same size, you can use memcpy :

 #include <string.h> char array1[28] = "a non null terminated string"; char array2[28]; ... memcpy(array2, array1, sizeof array2); 
0
Jun 21 '17 at 6:58
source share

Well, technically you can ...

 typedef struct { char xx[18]; } arr_wrap; char array1[18] = "abcdefg"; char array2[18]; *((arr_wrap *) array2) = *((arr_wrap *) array1); printf("%s\n", array2); /* "abcdefg" */ 

but it will not look very beautiful.

(Yes, the above code is always allowed to work and it is portable)

0
Apr 6 '19 at 19:16
source share



All Articles