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);
Answer from Vitor Villar on Stack Overflow
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;
}
๐ŸŒ
W3Schools
w3schools.in โ€บ c-programming โ€บ examples โ€บ copy-string-using-pointers
C Program to Copy String Using Pointers - W3Schools
C program to Copy string without using strcmp() function by creating our own function which uses pointers. ... #include<stdio.h> void copy_string(char*, char*); main() { char source[100], target[100]; printf("Enter source string\n"); gets(source); copy_string(target, source); printf("Target ...
๐ŸŒ
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
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ c-programming โ€บ 145454-copy-string-pointer.html
copy string by pointer
January 24, 2012 - Why should it work? You're setting one pointer "ta" to the address of another "so". These pointers are local to xcopy and changes to them have no impact on the arrays s and t back in main. Then, there is the fact that you are passing in arrays and not pointers so how would that even work?
๐ŸŒ
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 strcpy() from <string.h> header file to copy one string to the other. strcpy() accepts a pointer to the destination array and source array as a parameter and after copying it returns a pointer to the destination ...
๐ŸŒ
Techcrashcourse
techcrashcourse.com โ€บ 2014 โ€บ 10 โ€บ c-program-copy-string.html
C Program to Copy a String
It returns a pointer to the copy string destination. The strcpy() function copies the characters of source string into destination string, including null character. source must be a char pointer to a string terminated by a null character.
๐ŸŒ
E Computer Notes
ecomputernotes.com โ€บ home โ€บ lab โ€บ c program copy one string to another using pointers
C Program Copy One String to Another using Pointers - Computer Notes
February 9, 2013 - Problem Statement:- This program User asks to Copy String from one To another string with the Help of Pointer. Declare pointer type variable. Using While Statement. Display result on the screen. This C program is successfully compiled and run on a System. Output is shown below ยท #include<stdio.h> void copystr(char*,char*); void main() { char*str1="I am Dinesh Thakur"; char str2[30]; clrscr(); copystr(str2,str1); printf("\n %s",str2); getch(); } void copystr(char *dest,char *src) { while(*src!='\0') *dest++=*src++; *dest='\0'; return; } Output : I am Dinesh Thakur
Find elsewhere
๐ŸŒ
Quora
quora.com โ€บ How-do-I-write-a-C-program-to-copy-the-content-of-one-string-into-another-string-using-pointer-and-function
How to write a C program to copy the content of one string into another string using pointer and function - Quora
Answer (1 of 2): [code]#include void stringCopy(char *,char *); int main() { char a[5]="Hi!!"; char b[5]; stringCopy(a,b); printf("%s",b); return 0; } void stringCopy(char *src , char *dest) { while(*src!='\0') { *dest = *src; src++; dest++; } *dest='\0'; } [/code]
๐ŸŒ
OverIQ
overiq.com โ€บ c-programming-101 โ€บ the-strcpy-function-in-c
The strcpy() Function in C - C Programming Tutorial - OverIQ.com
The strcpy() function is used to copy strings. It copies string pointed to by source into the destination. This function accepts two arguments of type pointer to char or array of characters and returns a pointer to the first string i.e destination.
๐ŸŒ
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 - 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.
๐ŸŒ
Programming Simplified
programmingsimplified.com โ€บ c โ€บ source-code โ€บ c-program-copy-strings
String copy in C | Programming Simplified
void copy_string(char d[], char s[]) { int c = 0; while (s[c] != '\0') { d[c] = s[c]; c++; } d[c] = '\0'; } Function to copy a string using pointers.
๐ŸŒ
ProCoding
procoding.org โ€บ c-program-to-copy-one-string-to-another-using-pointers
C program to copy one string to another using pointers | ProCoding
March 22, 2024 - Copied string: Hello, world! Here, we are passing the the reference of source and destination string, not just the value that's why actual string gets manipulated. Because pointers refer to the address of the start/first element for string and array. So, there is no need to pass by reference using &. Arrays and String are always pass by reference in C.
๐ŸŒ
Rip Tutorial
riptutorial.com โ€บ copying strings
C Language Tutorial => Copying strings
#include <stdio.h> int main(void) { int a = 10, b; char c[] = "abc", *d; b = a; /* Integer is copied */ a = 20; /* Modifying a leaves b unchanged - b is a 'deep copy' of a */ printf("%d %d\n", a, b); /* "20 10" will be printed */ d = c; /* Only copies the address of the string - there is still only one string stored in memory */ c[1] = 'x'; /* Modifies the original string - d[1] = 'x' will do exactly the same thing */ printf("%s %s\n", c, d); /* "axc axc" will be printed */ return 0; } The above example compiled because we used char *d rather than char d[3]. Using the latter would cause a compiler error. You cannot assign to arrays in C.
๐ŸŒ
Connect2Compute
connect2compute.wordpress.com โ€บ c-programs โ€บ to-copy-one-string-to-another-using-pointers
To copy one string to another using pointers โ€“ Connect2Compute
February 23, 2016 - Enter the string : murali The copied string is : murali ... Like Loading... ... c programs on file handling/Explain Random Access to file in C explain fseek( ) Function ftell( ) Function with example
๐ŸŒ
Cplusplus
cplusplus.com โ€บ forum โ€บ beginner โ€บ 143358
Copying string using pointers - C++ Forum
September 26, 2014 - write one function strcpy1(char *src, char * dest), that makse a copy of str using arrays. ... Is there a way to do something like : char str2[str.length()]; I know the above is wrong โ€ฆ.anything similar i could do ? ... Yes, use C++ string instead of null-terminated character arrays.
๐ŸŒ
Vultr
docs.vultr.com โ€บ clang โ€บ examples โ€บ copy-string-without-using-strcpy
C Program to Copy String Without Using strcpy() | Vultr Docs
December 4, 2024 - Characters are copied one by one until the null character '\0' is encountered, indicating the end of the string. Initialize the source string and an empty destination string. Use a loop to iterate through the source string, advancing the pointer ...
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ c-copying-data-using-the-memcpy-function-in-c
C: Copying data using the memcpy() function in C
Letโ€™s look at a simple example of copying a string from one array to another using the memcpy() function. In case of overlapping memory segments, the memcpy() function ceases to work properly, i.e.