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. e.g. -

#define _CRT_SECURE_NO_WARNINGS
#include 
#include 

int main()
{
    char str1[] = "Hello";
    char str2[10] = {0};

    for (int x = 0; x < strlen(str1); ++x)
    {
        str2[x] = str1[x];
    }

    printf("%s\n", str2);

    return 0;
}

To make this common task easier there are standard library functions provided
which will perform this operation. e.g. - memcpy(), etc.

memcpy(str2, str1, 6);

When the array contains a nul-terminated string of characters you can use
strcpy(), etc.

strcpy(str2, str1);

Caveat: Some of the above functions are considered unsafe as they do not guard
against buffer overruns of the source and destination arrays. There are safer
versions provided by the compiler.

Note that if and when you start learning C++ you will find that there you can
assign a C++ std::string object to another object of the same type. However,
even in C++ the same rules apply when working with C strings, "raw" character
arrays, etc.

  • Wayne
Answer from WayneAKing on learn.microsoft.com
๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ how to copy the contents of an array of strings(a double char pointer returned from a function) into another array of strings?
r/C_Programming on Reddit: How to copy the contents of an array of strings(a double char pointer returned from a function) into another array of strings?
January 2, 2023 - Then allocate another array of (at least) the same length, and copy each element (i.e. char *) one by one using a conventional loop; as an alternative, the copying of elements can also be done via memcpy (something like memcpy(dest, src, sizeof (char *) * len)). ... How to store values in a array inside other function. ... Why can a local struct (possibly containing an array) be returned from a function, but not a local array directly? ... New to C, did a string interning library.
๐ŸŒ
YouTube
youtube.com โ€บ sanjay gupta tech school
Copy a string into another using pointer in c programming | by Sanjay Gupta - YouTube
Find Here: Links of All C language Video's Playlists/Video SeriesC Interview Questions & Answers | Video Serieshttps://www.youtube.com/watch?v=UlnSqMLX1tY&li...
Published ย  March 6, 2018
Views ย  14K
๐ŸŒ
W3Schools
w3schools.in โ€บ c-programming โ€บ examples โ€บ copy-string-using-pointers
C Program to Copy String Using Pointers - W3Schools
Then the printf() is used to display the message Enter source string\n. Then the gets() function is used to take the string from the user and store it in the character array name source. Then you have to call the user defined function copy_string(target, source); from within the main() which ...
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 41221381
copying array of string pointers to a new array in C - Stack Overflow
So if u want to make a real copy ... you will probably do a basic method wich mean to first allocate your double pointeur (char **array) to receive your new content....
Find elsewhere
Top answer
1 of 5
2

The array dict is an array of pointers to string literals.

In this statement

strcpy(dict[1],newWord1);

you are trying to copy one string literal in another string literal.

String literals are non-modifiable in C and C++. Any attempt to modify a strig literal results in undefined behavior of the program.

From the C Standard (6.4.5 String literals)

7 It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.

You should declare a two-dimensional character array if you are going to copy string literals in its elements or a one dimensional array of dynamically allocated character arrays.

The program can look the following way

#include <stdio.h>
#include <string.h>

#define N   9

size_t numberOfWordsInDict( char dict[][N] )
{
    size_t n = 0;

    while ( *dict[n] ) ++n;

    return n;
}

void printDict( char dict[][N] )
{
    printf("Dictionary:\n");

    size_t n = numberOfWordsInDict( dict );
    if ( n == 0 )
    {
        printf("The dictionary is empty.\n");
    } 
    else
    {
        for ( size_t i = 0; i < n; i++ )
        {
            printf( "- %s\n", dict[i] );
        }
    }
}

int main(void) 
{
    char dict[10][9] = 
    {
        "aap", "bro ", "jojo", "koe", "kip", "haha", "hond", "    drop"
    };
    char *newWord1 = "hoi";

    printDict(dict);
    strcpy(dict[1], newWord1);
    printDict(dict);

    return 0;
}

The program output is

Dictionary:
- aap
- bro 
- jojo
- koe
- kip
- haha
- hond
-     drop
Dictionary:
- aap
- hoi
- jojo
- koe
- kip
- haha
- hond
-     drop

Or you could use a Variable Length Array.

Keep in mind that according to the C Standard the function main without parameters shall be declared like

int main( void )
2 of 5
1

Since "bro" is a string constant and dict[1] points to it, dict[1] points to a string constant.

When you use strcpy(dict[1]), ..., you are trying to modify the thing that dict[1] points to. But it points to a constant. So you are trying to modify a constant.

Constants cannot be modified.

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c++ โ€บ different-ways-to-copy-a-string-in-c-c
Different ways to copy a string in C/C++ - GeeksforGeeks
July 23, 2025 - We can use the inbuilt function strncpy() from <string.h> header file to copy one string to the other. The strcnpy() function accepts a pointer to the destination array and source array as a parameter and the maximum number of characters to ...
Top answer
1 of 7
21

To copy strings in C, you can use strcpy. Here is an example:

#include <stdio.h>
#include <string.h>

const char * my_str = "Content";
char * my_copy;
my_copy = malloc(sizeof(char) * (strlen(my_str) + 1));
strcpy(my_copy,my_str);

If you want to avoid accidental buffer overflows, use strncpy instead of strcpy. For example:

const char * my_str = "Content";
const size_t len_my_str = strlen(my_str) + 1;
char * my_copy = malloc(len_my_str);
strncpy(my_copy, my_str, len_my_str);
2 of 7
17

To perform such manual copy:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char* orig_str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    char* ptr = orig_str;

    // Memory layout for orig_str:
    // ------------------------------------------------------------------------
    // |0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|  --> indices
    // ------------------------------------------------------------------------
    // |A|B|C|D|E|F|G|H|I|J|K |L |M |N |O |P |Q |R |S |T |U |V |W |X |Y |Z |\0|  --> data
    // ------------------------------------------------------------------------

    int orig_str_size = 0;
    char* bkup_copy = NULL;

    // Count the number of characters in the original string
    while (*ptr++ != '\0')
        orig_str_size++;        

    printf("Size of the original string: %d\n", orig_str_size);

    /* Dynamically allocate space for the backup copy */ 

    // Why orig_str_size plus 1? We add +1 to account for the mandatory 
    // '\0' at the end of the string.
    bkup_copy = (char*) malloc((orig_str_size+1) * sizeof(char));

    // Place the '\0' character at the end of the backup string.
    bkup_copy[orig_str_size] = '\0'; 

    // Current memory layout for bkup_copy:
    // ------------------------------------------------------------------------
    // |0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|  --> indices
    // ------------------------------------------------------------------------
    // | | | | | | | | | | |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |\0|  --> data
    // ------------------------------------------------------------------------

    /* Finally, copy the characters from one string to the other */ 

    // Remember to reset the helper pointer so it points to the beginning 
    // of the original string!
    ptr = &orig_str[0]; 
    int idx = 0;
    while (*ptr != '\0')
        bkup_copy[idx++] = *ptr++;

    printf("Original String: %s\n", orig_str);   
    printf("Backup String: %s\n", bkup_copy);

    return 0;
}
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ c-copying-data-using-the-memcpy-function-in-c
C: Copying data using the memcpy() function in C
In C, the memcpy() function copies n number of characters from one block of memory to another. ... Destination: Pointer to the destination array where data will be copied. Source: Pointer to the source of the data to copy.
๐ŸŒ
GNU
gnu.org โ€บ software โ€บ libc โ€บ manual โ€บ html_node โ€บ Copying-Strings-and-Arrays.html
The GNU C Library - GNU Project - Free Software Foundation (FSF)
This glibc manual version 2.43 (latest) is available in the following formats: ยท The manual is available for the following releases of glibc:
๐ŸŒ
Quora
quora.com โ€บ How-do-I-copy-a-string-in-C
How to copy a string in C - Quora
Answer (1 of 2): Three major ways. 1. strncpy [code]char* src = "a const string to be copied"; char dest[28] = {0}; char *strncpy(char *dest, const char *src, size_t n); dest[n]= '\0'; // terminate manually [/code] 1. strncpy copies a char array src into another char array dest up to a given ...
๐ŸŒ
Tutorials
onlinetutorialhub.com โ€บ home โ€บ c language โ€บ copy string using pointers in c programming language
Copy String Using Pointers In C Programming Language
May 26, 2025 - This example shows how to traverse ... (to and from). Pointer arithmetic (moving the pointer to the next character) is done with the ++ operator, whereas dereferencing (accessing the character at the current pointer location) is done with the * operator. The loop keeps going until the null character \0 is copied, which indicates that the copied string is finished. This demonstrates how pointers and character arrays are closely ...
Top answer
1 of 2
28

There are two way of working with array of characters (strings) in C. They are as follows:

char a[ROW][COL];
char *b[ROW];

Pictorial representation is available as an inline comment in the code.

Based on how you want to represent the array of characters (strings), you can define pointer to that as follows

    char (*ptr1)[COL] = a;
    char **ptr2 = b;

They are fundamentally different types (in a subtle way) and so the pointers to them is also slightly different.

The following example demonstrates the different ways of working with strings in C and I hope it helps you in better understanding of array of characters (strings) in C.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define ROW 5
#define COL 10

int main(void) 
{
    int i, j;
    char a[ROW][COL] = {"string1", "string2", "string3", "string4", "string5"};
    char *b[ROW];

    /*

    a[][]

      0   1   2   3   4   5   6     7    8   9
    +---+---+---+---+---+---+---+------+---+---+
    | s | t | r | i | n | g | 1 | '\0' |   |   |
    +---+---+---+---+---+---+---+------+---+---+
    | s | t | r | i | n | g | 2 | '\0' |   |   |
    +---+---+---+---+---+---+---+------+---+---+
    | s | t | r | i | n | g | 3 | '\0' |   |   |
    +---+---+---+---+---+---+---+------+---+---+
    | s | t | r | i | n | g | 4 | '\0' |   |   |
    +---+---+---+---+---+---+---+------+---+---+
    | s | t | r | i | n | g | 5 | '\0' |   |   |
    +---+---+---+---+---+---+---+------+---+---+

    */  

    /* Now, lets work on b */    
    for (i=0 ; i<5; i++) {
        if ((b[i] = malloc(sizeof(char) * COL)) == NULL) {
            printf("unable to allocate memory \n");
            return -1;
        }
    }

    strcpy(b[0], "string1");
    strcpy(b[1], "string2");
    strcpy(b[2], "string3");
    strcpy(b[3], "string4");
    strcpy(b[4], "string5");

    /*

       b[]              0   1   2   3   4   5   6    7     8   9
    +--------+        +---+---+---+---+---+---+---+------+---+---+
    |      --|------->| s | t | r | i | n | g | 1 | '\0' |   |   |
    +--------+        +---+---+---+---+---+---+---+------+---+---+
    |      --|------->| s | t | r | i | n | g | 2 | '\0' |   |   |
    +--------+        +---+---+---+---+---+---+---+------+---+---+
    |      --|------->| s | t | r | i | n | g | 3 | '\0' |   |   |
    +--------+        +---+---+---+---+---+---+---+------+---+---+
    |      --|------->| s | t | r | i | n | g | 4 | '\0' |   |   |
    +--------+        +---+---+---+---+---+---+---+------+---+---+
    |      --|------->| s | t | r | i | n | g | 5 | '\0' |   |   |
    +--------+        +---+---+---+---+---+---+---+------+---+---+

    */

    char (*ptr1)[COL] = a;
    printf("Contents of first array \n");
    for (i=0; i<ROW; i++)
        printf("%s \n", *ptr1++);


    char **ptr2 = b;
    printf("Contents of second array \n");
    for (i=0; i<ROW; i++)
        printf("%s \n", ptr2[i]);

    /* b should be free'd */
    for (i=0 ; i<5; i++)
        free(b[i]);

    return 0;
}
2 of 2
1

What would be the correct way to solve this problem?

Well, the correct way would be to use a library specifically designed for dealing with multilanguage interfaces - for instance gettext.

Another way, though patchier, would be to use a hash table (also known as "dictionary" or "hash map" or "associative map" in other languages/technologies): Looking for a good hash table implementation in C

It's probably not the answer you were looking for, but you've asked the wrong question to the right problem.