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
🌐
GeeksforGeeks
geeksforgeeks.org › c language › how-to-convert-a-string-to-a-char-array-in-c
How to Convert a String to a Char Array in C? - GeeksforGeeks
July 23, 2025 - In this article, we will learn how to convert a string to a char array in C. The most straightforward method to convert a string to a char array is by using strcpy() function. Let's take a look at an example: ... #include <stdio.h> #include ...
Discussions

Copying string into char array. - C++ Forum
Copying string into char array. ... Description: I have to pass a c string into a char array, but if the string is larger then the char array size then it would copy the string array to the just the char array size then end it with a null character. More on cplusplus.com
🌐 cplusplus.com
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
Copying string literals in C into an character array - Stack Overflow
I have a string literal: char *tmp = "xxxx"; I want to copy the string literal into an array. For example: how do I copy tmp into an char array[50]? and how to copy one string literal to another? More on stackoverflow.com
🌐 stackoverflow.com
How to Copy a Char Array into another Ch - C++ Forum
Is there a way to only copy the filled part of the array before '\0'? Or would I need to get the input as a string, get the length of the string, put the string into an array of char, then copy into another array? ... You can use the strlen() function before you begin the copying loop. More on cplusplus.com
🌐 cplusplus.com
🌐
Cplusplus
cplusplus.com › forum › beginner › 39119
Copying string into char array. - C++ Forum
Or if you are trying to convert a character array into a string, this can be easily accomplished with the string concatanation (spelling?) operator: +, and a for loop.
Top answer
1 of 4
10

Use strcpy(), strncpy(), strlcpy() or memcpy(), according to your specific needs.

With the following variables:

const char *tmp = "xxxx";
char buffer[50];

You typically need to ensure your string will be null terminated after copying:

memset(buffer, 0, sizeof buffer);
strncpy(buffer, tmp, sizeof buffer - 1);

An alternative approach:

strncpy(buffer, tmp, sizeof buffer);
buffer[sizeof buffer - 1] = '\0';

Some systems also provide strlcpy() which handles the NUL byte correctly:

strlcpy(buffer, tmp, sizeof buffer);

You could naively implement strlcpy() yourself as follows:

size_t strlcpy(char *dest, const char *src, size_t n)
{
    size_t len = strlen(src);

    if (len < n)
        memcpy(dest, src, len + 1);
    else {
        memcpy(dest, src, n - 1);
        dest[n - 1] = '\0';
    }

    return len;
}

The above code also serves as an example for memcpy().

Finally, when you already know that the string will fit:

strcpy(buffer, tmp);
2 of 4
3

Use strcpy, it is pretty much well documented and easy to find:

const char* tmp = "xxxx";
// ...
char array[50];
// ...
strcpy(array, tmp);

But of course, you must make sure that the length of the string that you are copying is smaller than the size of the array. An alternative then is to use strncpy, which gives you an upper limit of the bytes being copied. Here's another example:

const char* tmp = "xxxx";
char array[50];
// ...
array[49] = '\0';
strncpy(array, tmp, 49); // copy a maximum of 49 characters

If the string is greater than 49, the array will still have a well-formed string, because it is null-terminated. Of course, it will only have the first 49 characters of the array.

🌐
Cplusplus
cplusplus.com › reference › string › string › copy
std::string::copy
Pointer to an array of characters. The array shall contain enough storage for the copied characters. ... Number of characters to copy (if the string is shorter, as many characters as possible are copied).
Find elsewhere
🌐
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
Forgetting to add a NUL terminator when copying into C string buffers. Summary checklist: choose strcpy/strlcpy for NUL-terminated strings when sizes are controlled; memcpy/memmove for raw bytes; prefer std::string or safe helpers in C++ to avoid manual buffer management. ... we have two methods in c one is strcpy and another is iterating each character and assign to another array...
🌐
Delft Stack
delftstack.com › home › howto › c array copy in c
How to Copy Char Array in C | Delft Stack
February 2, 2024 - The memcpy function is part of the standard library string utilities defined in <string.h> header file. It takes three parameters, the first of which is the destination pointer, where the contents of the array will be copied.
🌐
Cplusplus
cplusplus.com › forum › beginner › 111242
How to Copy a Char Array into another Ch - C++ Forum
Is there a way to only copy the filled part of the array before '\0'? Or would I need to get the input as a string, get the length of the string, put the string into an array of char, then copy into another array? ... You can use the strlen() function before you begin the copying loop.
🌐
CodinGame
codingame.com › playgrounds › 14213 › how-to-play-with-strings-in-c › string-copy
String Copy - How to play with strings in C
CodinGame is a challenge-based training platform for programmers where you can play with the hottest programming topics. Solve games, code AI bots, learn from your peers, have fun.
Top answer
1 of 2
3

First off struct employee* emps[maxemps]; creates an array of pointers to struct employee with array size maxemps. You haven't actually set aside any space in memory for the actual structs, just the pointers that will point to them. To dynamically allocate space on the heap for your structs so you can use them in a meaningful way, you'll need to loop over a call to malloc() like so:

for (i = 0; i < maxemps; i++) {
   emps[i] = malloc(sizeof(struct employee));
}

You'll also want a similar loop at the end of your program which will free() each pointer.

Next, when you are getting input from a user you really want to be using fgets() over scanf() because fgets() allows you to specify the number of characters to read in so you can prevent overflows on your destination buffer.

Update

If you want to work with a single struct employee without the use of pointers this is accomplished by declaring one or more struct employee on the stack and then using the . member access operator as follows:

struct employee emp;
fgets(emp.lastname, sizeof(emp.lastname), stdin);

Update2

I found a number of errors in your code. Please see this link for a working sample with comments.

2 of 2
2

You only need:

scanf("%10s", (emps[i])->last_name);

Here "%10s" denotes a string with a maximum length of 10, and it will load the string to last_name.

In C the string is represented as a char array, with '\0' as the end.

Using scanf here is vulnerable to buffer attacks if the user-input is longer than 10: http://en.wikipedia.org/wiki/Scanf#Security, so you need to assign a maximum length to the format.

🌐
Cplusplus
cplusplus.com › reference › cstring › strcpy
strcpy - cstring
Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).
🌐
Quora
quora.com › What-are-the-steps-to-convert-a-string-to-a-char-array-in-CPP
What are the steps to convert a string to a char array in CPP? - Quora
This can be done with the help of c_str() and strcpy() function. The c_str() function is used to return a pointer to an array that contains a null terminated sequence of character representing the current value...
🌐
TutorialsPoint
tutorialspoint.com › convert-string-to-char-array-in-cplusplus
How to convert string to char array in C++?
May 28, 2025 - #include <iostream> #include <string> using namespace std; int main() { string str = "Tutorialspoint"; char c[str.size() + 1]; str.copy(c, str.size() + 1); c[str.size()] = '\0'; cout << c << '\n'; return 0; }
🌐
Quora
quora.com › What-are-the-steps-to-convert-String-to-Char-Array-in-C
What are the steps to convert String to Char Array in C? - Quora
Answer (1 of 12): As the answers say, a string is a char array, mostly. This answer is a bit wrong in some contexts. I am thinking of string constants specifically. A string constant should probably not be treated as a char array in terms of modifying it. If I say char *p = “string constant”; ...