You can't directly do array2 = array1, because in this case you manipulate the addresses of the arrays (char *) and not of their inner values (char).

What you, conceptually, want is to do is iterate through all the chars of your source (array1) and copy them to the destination (array2). There are several ways to do this. For example you could write a simple for loop, or use memcpy.

That being said, the recommended way for strings is to use strncpy. It prevents common errors resulting in, for example, buffer overflows (which is especially dangerous if array1 is filled from user input: keyboard, network, etc). Like so:

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

As @Prof. Falken mentioned in a comment, strncpy can be evil. Make sure your target buffer is big enough to contain the source buffer (including the \0 at the end of the string).

Answer from aymericbeaumet on Stack Overflow
🌐
Cplusplus
cplusplus.com › forum › beginner › 111242
How to Copy a Char Array into another Ch - C++ Forum
You don't need a special function like strcpy(). All you are doing is copying one character to another, so just use =. ... Thanks for the reply. I am stepping through the program and found that the entire array is copied (including the null values) instead of just the part of the array that ...
Discussions

Copy string in C
A C string is a nul-terminated character array. The C language does not allow assigning the contents of an array to another array. As noted by Barry, you must copy the individual characters one by one from the source array to the destination array. More on learn.microsoft.com
🌐 learn.microsoft.com
3
0
September 12, 2020
How to copy a string into an array of chars without using any built-in function?
If you already have an array of strings, a string being an array of char that ends with "\0", you can just iterate over a[0] and it will iterate over "Hello" ? Now if you need to copy it, just create another array with malloc and copy every char by iterating over a[0] More on reddit.com
🌐 r/C_Programming
14
0
December 9, 2023
c - How can I copy individual chars from a char** into another char**? - Stack Overflow
So now I just want to clean it up and strip out the unnecessary characters! I thought I would make a new array and copy over the letters from the old array so long as they aren't [ - #. But for the life of me I cannot work out how to copy the right letters into another array!! More on stackoverflow.com
🌐 stackoverflow.com
c - Copying multi char array to another multi char array - Stack Overflow
– David C. Rankin Commented Jun 26, 2015 at 22:41 ... Further, sizeof(a) is meaningless in your arrayCopy function (well, not meaningless -- it just reports the size of the character pointer a which is always 8 on 64-bit boxes and 4 on x86 boxes. More on stackoverflow.com
🌐 stackoverflow.com
🌐
Quora
quora.com › How-do-I-copy-a-character-array-to-another-char-array
How to copy a character array to another char array - Quora
Summary checklist: choose ... buffer management. ... we have two methods in c one is strcpy and another is iterating each character and assign to another array....
🌐
Cprogramming
cboard.cprogramming.com › c-programming › 61490-char-copy.html
char copy - C Board - Cprogramming.com
February 6, 2005 - char name[256]; strcpy(name,"a"); Or another way: Code: char name[256]; name[0] = 'a'; name[1] = '\0'; http://www.freechess.org · 02-06-2005 #3 · Hammer · View Profile · View Forum Posts · Visit Homepage End Of Line · Join Date · Apr 2002 · Posts · 6,231 · >>how can i copy a single ...
🌐
LinuxQuestions.org
linuxquestions.org › questions › programming-9 › copy-a-char*-to-another-char*-74826
Copy a char* to another char*
July 22, 2003 - Hello Guys, I read in a book that to copy a char array to another char array, we can just copy the memory location. The program is : #include
🌐
Delft Stack
delftstack.com › home › howto › c array copy in c
How to Copy Char Array in C | Delft Stack
February 2, 2024 - The second parameter is the pointer to the source array, and the last parameter specifies the number of bytes to be copied. Notice that the sizeof operator returns the char array object sizes in bytes. So we allocate the dynamic memory with malloc call passing the sizeof arr expression value to it.
Find elsewhere
🌐
Quora
quora.com › How-can-we-copy-one-char-array-into-another-char-array-in-C
How can we copy one char array into another char array in C++? - Quora
Answer: This is a tricky problem. If you don't know the sizes of the two arrays you are in trouble. Even if you do know the sizes of the two arrays the operation only makes sense if both are the same size. If the from array is larger than the to array you risk overwriting memory, if the to array ...
🌐
Stack Overflow
stackoverflow.com › questions › 31083056 › copying-multi-char-array-to-another-multi-char-array
c - Copying multi char array to another multi char array - Stack Overflow
After viewing similar questions ... a method called arrayCopy. ... void arrayCopy(char * a, char * b) { struct universe data; int r; for(r = 0; r < data.rows; r++) memcpy(b, a, sizeof(a)); }...
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Copying block of chars to another char array in a specific location - Programming - Arduino Forum
September 29, 2020 - For example : char alphabet[26] = "abcdefghijklmnopqrstuvwxyz"; char letters[3]="MN"; How can I copy "MN" from the second array and replace "mn" in the first array ? if I declare the first array this way : char array[26] = "abcdefghijklmnopqrstuvwxyz"; Is there any difference between these two initializations?
Top answer
1 of 3
1

You will always need to 'worry' about the null terminator, in the sense that you can't not have a null terminator in your destination C string, and you will always need to explicitly write the null terminator at the end of your new C string.

Even the built-in strcpy method will copy the null terminator character from your original string to the destination string.

#include <stdio.h>
#include <assert.h>

int copy(char * source, char * destination, unsigned int lengthDestination);

int copy(char * source, char * destination, unsigned int lengthDestination)
{
    int i;
    for(i = 0; source[i] != '\0'; i++) {
        destination[i] = source[i];
    }
    assert(i+1 == lengthDestination);
    destination[i+1] = '\0';
    return 0;
}

int main() {
    char * source = "Test number 17"; //Length is 15 counting the null character
    unsigned int destinationLength = 15;
    char destination[destinationLength];
    copy(source, destination, destinationLength);
    printf("The String source is: %s\n", source);
    printf("The String destination is: %s\n", destination);
    return 0;
}
2 of 3
1

If you are passing destination previously declared with automatic storage type or as a pointer previously allocated with malloc, calloc or realloc having allocated storage type, and in either case having nchars of storage available, you can implement a fairly robust copy function by simply using snprintf.

Since you are passing the maximum number of characters, including the nul-terminating character as a parameter to your copy function, that dovetails nicely with the size parameter for snprintf. Further, snprintf will insure a nul-terminated string in destination -- even if source is too long to fit in destination. As a benefit, snprintf returns the number of characters copied if there is sufficient storage in destination for source, otherwise it returns the number of characters that would have been copied had destination had sufficient space -- allowing you to determine the number of characters truncated if destination is insufficient to hold your source string.

Before looking at the implementation of copy let's look at a prototype and talk about declaring functions to that they provide a meaningful return, and while up to you, let's also look at the order of parameters and type qualifier for source, e.g.

/* simple strcpy src to dest, returns dest on success and number of chars
 * (including nul-termining char) in nchar, returns NULL otherwise.
 */
char *copy (char *dest, const char *src, size_t *nchar);

If you notice, most string functions return a pointer to the destination string on success (or NULL otherwise) which allows you to make immediate use of the return. Next, while not a show-stopper, most string (or memory in general) copy functions place the destination as the first parameter, followed later by the source. I'm sure either Brian Kerrigan or Dennis Ritchie could explain why, but suffice it to say, most copy function parameters are ordered that way.

Notice also that since you are not modifying the source string in copy, it is best to qualify the parameter as const. The const qualifier is more or less a promise you make to the compiler that source will not be modified in copy which allows the compiler to warn if it sees you breaking that promise, while also allowing the compiler to further optimize the function knowing source will not change.

Finally notice your size or my nchar is passed as a pointer above instead of an immediate value. Since a function in C can only return a single value, if you need a way to get a second piece of information back to the caller, pass a pointer as a parameter so the value at that address can be updated within the function making the new value available to the calling function. Here you return a pointer to dest (or NULL) to indicate success/failure while also updating nchar to contain the number of characters in dest (including the nul-terminating character as you passed in a size not a length).

The definition of copy is quite short and simplistic. The only requirement is the source and destination strings not overlap. (neither strcpy or snprintf are defined in that case). The basic flow is to validate both src and dest are not NULL, then handle the case where src is the "empty-string" (e.g. 1st character is the nul-character) and then to copy src to dest using snprintf saving the return in written and then using a simple conditional to determine whether truncation occurred (and warning in that case) and concluding by updating the value pointed to by nchar and returning dest, e.g.

/* simple strcpy src to dest, returns dest on success and number of chars
 * (including nul-termining char) in nchar, returns NULL otherwise.
 */
char *copy (char *dest, const char *src, size_t *nchar)
{
    if (!src || !dest) {    /* validate src & dest not NULL */
        fputs ("error: src or dest NULL\n", stderr);
        return NULL;        /* return NULL on error */
    }

    if (!*src)  /* handle src being an "empty-string" */
        *dest = 0, *nchar = 0;

    int written = snprintf (dest, *nchar, "%s", src);   /* call snprintf */

    if ((size_t)written + 1 > *nchar) { /* handle truncated case */
        fprintf (stderr, "warning: dest truncated by %zu chars.\n",
                (size_t)(written + 1) - *nchar);    /* warn with count */
    }
    else     /* src fit in dest, set nchar to no. of chars in dest */
        *nchar = (size_t)(written + 1); /* including nul-character */

    return dest;     /* return dest so available for immediate use */
}

Putting it altogether in a short example that takes the string to copy as the first argument to the program (using "source string" by default if no argument is given), you could do something like the following:

#include <stdio.h>

#define MAXC 16     /* constant for destination length */

/* simple strcpy src to dest, returns dest on success and number of chars
 * (including nul-termining char) in nchar, returns NULL otherwise.
 */
char *copy (char *dest, const char *src, size_t *nchar)
{
    if (!src || !dest) {    /* validate src & dest not NULL */
        fputs ("error: src or dest NULL\n", stderr);
        return NULL;        /* return NULL on error */
    }

    if (!*src)  /* handle src being an "empty-string" */
        *dest = 0, *nchar = 0;

    int written = snprintf (dest, *nchar, "%s", src);   /* call snprintf */

    if ((size_t)written + 1 > *nchar) { /* handle truncated case */
        fprintf (stderr, "warning: dest truncated by %zu chars.\n",
                (size_t)(written + 1) - *nchar);    /* warn with count */
    }
    else     /* src fit in dest, set nchar to no. of chars in dest */
        *nchar = (size_t)(written + 1); /* including nul-character */

    return dest;     /* return dest so available for immediate use */
}

int main (int argc, char **argv) {

    char *src = argc > 1 ? argv[1] : "source string",
        dest[MAXC];
    size_t n = MAXC;

    if (copy (dest, src, &n))
        printf ("dest: '%s' (%zu chars including nul-char)\n", dest, n);
}

(note: the maximum number of characters in dest is kept short intentionally to easily show how truncation is handled -- size as appropriate for your needs)

Example Use/Output

$ ./bin/strcpy_snprintf
dest: 'source string' (14 chars including nul-char)

Showing maximum number of characters that can be copied without warning:

$ ./bin/strcpy_snprintf  123456789012345
dest: '123456789012345' (16 chars including nul-char)

Showing handling source too long for destination:

$ ./bin/strcpy_snprintf  1234567890123456
warning: dest truncated by 1 chars.
dest: '123456789012345' (16 chars including nul-char)

Look things over and let me know if you have further questions. There are at least a dozen different ways to approach a string copy, but given you are passing dest with its own storage and passing the maximum number of characters (including the nul-character) as a parameter, it's hard to beat snprintf in that case.

🌐
TutorialKart
tutorialkart.com › c-programming › how-to-copy-a-string-to-another-using-character-arrays-in-c
How to Copy a String to Another using Character Arrays in C
February 19, 2025 - #include <stdio.h> int main() { char source[] = "Hello, World!"; char destination[50]; // Large enough to hold the copied string int i; // Copy characters one by one for (i = 0; source[i] != '\0'; i++) { destination[i] = source[i]; } destination[i] = '\0'; // Add null terminator printf("Source String: %s\n", source); printf("Copied String: %s\n", destination); return 0; } ... We declare a character array source[] containing the string “Hello, World!”. We create an empty destination[50] array large enough to store the copied string.
🌐
CodeProject
codeproject.com › Questions › 1194027 › How-can-I-copy-a-char-array-in-another-char-array
How can I copy a char array in another char array?
July 28, 2017 - Do not try and find the page. That’s impossible. Instead only try to realise the truth - For those who code; Updated: 1 Jul 2007
🌐
CopyProgramming
copyprogramming.com › howto › c-c-copy-char-array-to-char-array
Pointers: Copying a char array into another char array
May 26, 2023 - Currently, you're copying strings into an uninitialized buffer. To avoid this, make sure to allocate memory for each of the three entries. ... Release them once you have completed your task. ... C++ copy a part of a char array to another char array, char * final_string; I would like to copy part of the second_string into the first_string.
🌐
Arduino Forum
forum.arduino.cc › projects › programming
How to copy a char array to a another char array - Programming - Arduino Forum
January 15, 2021 - I want to store a newly read RFID tag number which is not in the myTag array in to the EEPROM. for that I tried creating another char array named char_array to store the converted tag number which will be read by the arduino as a string. Then I tried to copy that array value to the main array ...