int size = sizeof(original) / sizeof(char*);

Applying sizeof to a pointer not an array over here. This will not return the size of the string to which it points to. strlen is the one you should look for here. Replace the above line with this (Solution to the size determination)

int size = strlen(original); // better to use `size_t`

Now here what is going on - the string literal is a char array which decays into pointer to the first element and that pointer value is being assigned - over which you applied sizeof. There is no trace of array here. To get the number of elements an array can hold you have to pass the array itself not the pointer.

Another thing is C is pass by value - so here to retain the changes either pass the pointer to the pointer or pass the allocated memory address from the function.

sizeof(char) is 1 so you don't need to write it explicitly - you can simply write

copy = malloc(size + 1);

Code:

stringcpy(original, &copy);

And

void stringcpy(const char* original, char** copy) {

    int size = strlen(original);
    *copy = malloc(size + 1);

    int i;
    for(i = 0; original[i] != '\0'; i++) {
        (*copy)[i] = original[i];
    }
    (*copy)[i] = '\0';
}

I have shown you the solution of the problem by passing the address of the pointer variable to the other function. Alternatively (try it yourself) you can return the allocated memory chunk's address from the function and assign it to the copy. This would also work.

Edit: If you are not allowed to change the function signature then yes it's not possible unless you provide the allocated memory to the copy like you did before in case-1.

The otherway to achieve correct behavior would be to do this

char* stringcpy(const char* original) {

    int size = strlen(original) ;
    char* copy = malloc(size + 1);

    int i;
    for(i = 0; original[i] != '\0'; i++) {
        copy[i] = original[i];
    }
    copy[i]=0;
    return copy;
}

And use it like this

copy = stringcpy(original);

Few things omitted in this answer:

  • Always check the return value of malloc - in case it returns NULL you won't dereference it if you put a check of it.

  • Don't cast the return type of malloc - char* to void* conversion is implicit.

  • Free the dynamically allocated memory when you are done working with it.

Answer from user2736738 on Stack Overflow
Top answer
1 of 2
3
int size = sizeof(original) / sizeof(char*);

Applying sizeof to a pointer not an array over here. This will not return the size of the string to which it points to. strlen is the one you should look for here. Replace the above line with this (Solution to the size determination)

int size = strlen(original); // better to use `size_t`

Now here what is going on - the string literal is a char array which decays into pointer to the first element and that pointer value is being assigned - over which you applied sizeof. There is no trace of array here. To get the number of elements an array can hold you have to pass the array itself not the pointer.

Another thing is C is pass by value - so here to retain the changes either pass the pointer to the pointer or pass the allocated memory address from the function.

sizeof(char) is 1 so you don't need to write it explicitly - you can simply write

copy = malloc(size + 1);

Code:

stringcpy(original, &copy);

And

void stringcpy(const char* original, char** copy) {

    int size = strlen(original);
    *copy = malloc(size + 1);

    int i;
    for(i = 0; original[i] != '\0'; i++) {
        (*copy)[i] = original[i];
    }
    (*copy)[i] = '\0';
}

I have shown you the solution of the problem by passing the address of the pointer variable to the other function. Alternatively (try it yourself) you can return the allocated memory chunk's address from the function and assign it to the copy. This would also work.

Edit: If you are not allowed to change the function signature then yes it's not possible unless you provide the allocated memory to the copy like you did before in case-1.

The otherway to achieve correct behavior would be to do this

char* stringcpy(const char* original) {

    int size = strlen(original) ;
    char* copy = malloc(size + 1);

    int i;
    for(i = 0; original[i] != '\0'; i++) {
        copy[i] = original[i];
    }
    copy[i]=0;
    return copy;
}

And use it like this

copy = stringcpy(original);

Few things omitted in this answer:

  • Always check the return value of malloc - in case it returns NULL you won't dereference it if you put a check of it.

  • Don't cast the return type of malloc - char* to void* conversion is implicit.

  • Free the dynamically allocated memory when you are done working with it.

2 of 2
0

The memory that you allocate inside the function is allocated from the heap. When you exit the function, that memory is given back to the system, so that it is no longer allocated when you return.

Discussions

copying string using dynamic memory
This keeps getting worse. (Sorry if I sound annoyed, please accept that as a feature of me being a grumpy old dude. Making mistakes when learning is completely normal and OK, you're not doing anything wrong by asking here. :) ) So anyway. These are sort of OK: char* buff1 = malloc(sizeof(s1) * strlen(s1) + 1); char* buff2 = malloc(sizeof(s2) * strlen(s2) + 1); There the sizeof(...) parts will evaluate to the size of a string pointer, so you're basically allocating 4 or 8 bytes for every nonzero character plus one byte for the terminating NUL. A char string takes up one byte per character. You only need strlen(s1) + 1 to copy s1. These aren't even valid C: char *strncpy(buff1, strings->first, strlen(s1) + 1); char *strncpy(buff2, strings->second, strlen(s2) + 1) Even if that were valid, then how do you suppose copying stuff from an invalid memory location (strings->...) to buff1 and buff2 would do any good? Take a brief moment to think what the program does here: return strings; free(buff1); free(buff2); What does return do? Considering that you're giving the computer very literal, exact instructions on what to do in that exact order, then what line will the program execute after return? P.S.: Always check if malloc() succeeded. Always. Never omit that step, it is not optional. (The exact same applies for all functions that allocate memory.) More on reddit.com
๐ŸŒ r/C_Programming
42
13
August 26, 2020
c - Copy a String using dynamic function - Stack Overflow
Im trying to copy a string into another string. Facing issue on final output. Please refer the expecting output and actual output. This is for copy a string in between another string. #include More on stackoverflow.com
๐ŸŒ stackoverflow.com
October 20, 2019
c++ - Proper way to copy C strings - Stack Overflow
Many implementations have a "short ... avoid dynamic allocation for short strings; in that case, there will be little or no overhead over using a C-style array. Access to individual characters is just as convenient as with a C-style array; in both cases, s[i] gives the character at position i as an lvalue. Copying becomes stringB ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
c - Copying dynamically allocated char* string into new char* string - Stack Overflow
I will appreciate him who can help me. Please note that I am programming in CLR (Common Language Run Time) with visual studio 2010. I have assigned character to a string with dynamic memory allocat... More on stackoverflow.com
๐ŸŒ stackoverflow.com
September 10, 2014
๐ŸŒ
YouTube
youtube.com โ€บ sanjay gupta
Copy a string into another string using dynamic memory allocation in c programming - YouTube
Find Here: Links of All C language Video's Playlists/Video Series C Interview Questions & Answers | Video Series https://www.youtube.com/watch?v=UlnSqMLX1tY&...
Published ย  March 15, 2018
Views ย  2K
๐ŸŒ
Miami
rabbit.eng.miami.edu โ€บ class โ€บ een218 โ€บ malloc.html
Dynamic Memory Allocation and You
Its prototype should be: char *safecopy(char *s);. You give it a string as a parameter, it creates a new string using heap memory, copies the original into it, and returns the new string as its result. Why would a function like this be useful? The question mentions heap memory, which is exactly ...
๐ŸŒ
RIT
se.rit.edu โ€บ ~swen-250 โ€บ slides โ€บ instructor-specific โ€บ Rabb โ€บ C โ€บ 06-C-Memory-Alloc-Strings.pdf pdf
Personal Software Engineering Memory Management in C (Dynamic Strings)
char *p2 = make_copy("world!") ; char *p3 = catenate(p1, p2) ; char *p4 = catenate("Hello, ", "world!") ; So what is the difference between the 2 calls to catenate? The constant strings have preallocated static storage. The dynamic strings (p1 and p2) are in dynamically allocated space.
๐ŸŒ
GitHub
github.com โ€บ antirez โ€บ sds
GitHub - antirez/sds: Simple Dynamic Strings library for C ยท GitHub
There is a similar function called sdscpy that does not need a length but expects a null terminated string instead. You may wonder why it makes sense to have a string copy function in the SDS library, since you can simply create a new SDS string from scratch with the new value instead of copying the value in an existing SDS string.
Starred by 5.4K users
Forked by 501 users
Languages ย  C 99.6% | Makefile 0.4%
๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ copying string using dynamic memory
r/C_Programming on Reddit: copying string using dynamic memory
August 26, 2020 -

the question asks to returns a pointer to a new dynamically allocated StringPair structure that contains pointers to two newly created copies of the parameter strings s1 and s2.

the function im working on is: StringPair* newStringPair(const char* s1, const char* s2)

my attempt:

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

// Declare the StringPair type.
// Note that we have incorporated the struct declaration into
// the typedef, but that this only works because we don't have any
// StringPair pointers in the structure (e.g. StringPair* next).
typedef struct stringpair_s {
    char* first;
    char* second;
 } StringPair;

// **** Insert your newStringPair function definition here ***
StringPair* newStringPair(const char* s1, const char* s2)
{
	StringPair* strings;
	strings->first = s1;
	strings->second = s2;
	char* buff1 = malloc(sizeof(s1) * strlen(s1) + 1);
	char* buff2 = malloc(sizeof(s2) * strlen(s2) + 1);
	char *strncpy(buff1, strings->first, strlen(s1) + 1);
	char *strncpy(buff2, strings->second, strlen(s2) + 1)
	return strings;
	free(buff1);
	free(buff2);
}

int main(void)
{
    char s1[] = "My first string";
    char s2[] = "Another one";
    StringPair* pair = NULL;

    pair = newStringPair(s1, s2);

    // Before printing, alter the initial strings to ensure
    // the function hasn't just copied the pointers.
    strncpy(s1, "Smasher1", strlen(s1)+1);
    strncpy(s2, "Clobber2", strlen(s2)+1);

    // Now print the new StringPair.
    printf("String pair: ('%s', '%s')\n", pair->first, pair->second);

    // Lastly free all dynamic memory involved.
    free(pair->first);
    free(pair->second);
    free(pair);
}
Top answer
1 of 5
13
This keeps getting worse. (Sorry if I sound annoyed, please accept that as a feature of me being a grumpy old dude. Making mistakes when learning is completely normal and OK, you're not doing anything wrong by asking here. :) ) So anyway. These are sort of OK: char* buff1 = malloc(sizeof(s1) * strlen(s1) + 1); char* buff2 = malloc(sizeof(s2) * strlen(s2) + 1); There the sizeof(...) parts will evaluate to the size of a string pointer, so you're basically allocating 4 or 8 bytes for every nonzero character plus one byte for the terminating NUL. A char string takes up one byte per character. You only need strlen(s1) + 1 to copy s1. These aren't even valid C: char *strncpy(buff1, strings->first, strlen(s1) + 1); char *strncpy(buff2, strings->second, strlen(s2) + 1) Even if that were valid, then how do you suppose copying stuff from an invalid memory location (strings->...) to buff1 and buff2 would do any good? Take a brief moment to think what the program does here: return strings; free(buff1); free(buff2); What does return do? Considering that you're giving the computer very literal, exact instructions on what to do in that exact order, then what line will the program execute after return? P.S.: Always check if malloc() succeeded. Always. Never omit that step, it is not optional. (The exact same applies for all functions that allocate memory.)
2 of 5
3
You can't do this: StringPair* strings; strings->first = s1; strings->second = s2; You're not allocating any memory for strings. You've just declared an uninitialized pointer (i.e., a pointer containing garbage) and tried to use that as if it's an address of some valid memory area.
Find elsewhere
๐ŸŒ
ZetCode
zetcode.com โ€บ clang โ€บ strcpy
C strcpy Tutorial: String Copying with Practical Examples
The strcpy function copies a null-terminated string from source to destination. It's declared in string.h and takes two parameters: destination and source pointers. strcpy copies until it encounters the null terminator. It doesn't check for buffer sizes, making it potentially unsafe.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ strcpy-in-c
strcpy() in C - GeeksforGeeks
March 6, 2026 - Explanation: This program demonstrates the use of the strcpy() function to copy strings in C. It copies the contents of s1 to s2, a predefined message to s3. The final result is printed to show the contents of all strings.
๐ŸŒ
Locklessinc
locklessinc.com โ€บ articles โ€บ dynamic_cstrings
Dynamic Strings in C
Strings in C are defined as a stream of contiguous bytes, terminated by a byte with the value zero. The C standard library has many functions that deal with this type of string, but they suffer from one major problem. The definition of a C string does not contain the size of the memory allocated ...
Top answer
1 of 2
1

sizeof(char) is always 1. You can simplify your code by eliminating it.

Once you know the size of the string you're about to read, there's no way using scanf(3) to limit the input it reads to the size of the buffer you allocated. In general, we don't rely on the input to describe itself accurately.

In your case, even if the description is accurate, the code isn't: scanf promises to terminate the string read by %s with a NUL character, so your 6-byte input needs a 7-byte buffer to accommodate that NUL. That is one error.

There is a fancy way to get scanf to allocate the string memory for you. Traditionally, though, you'd use getline(3) for that, and strlen(3) to find out how much it read.

As one comment points out, realloc(3) invalidates its first argument, meaning p1 can't be relied on after that call. You really want output to be a separate buffer, into which you'll copy data from p1 and p2.

Once you've allocated your output buffer, though, you may be surprised to learn that sprintf(3) is your friend. One call to that function, with %.*s constructions to limit string output dynamically, and pointer arithmetic on the inputs, will generate what you need.

2 of 2
0

Here is the correct code of copy a string using dynamic method. Refer the point 1 to get more clarity.

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

int main()
{
    int n1, n2, loc;
    char *p1, *p2, *output;

    printf("Enter size of p1: ");
    scanf("%d", &n1);

    p1 = malloc((n1 + 1) * sizeof(char));

    printf("Enter the P1 String: ");
    scanf("%s", p1);

    printf("\nEnter the size of p2: ");
    scanf("%d", &n2);

    p2 = malloc((n2 + 1) * sizeof(char));

    printf("Enter the P2 String: ");
    scanf("%s", p2);

    printf("\nEnter the Location to copy: ");
    scanf("%d", &loc);

    output = malloc((n1 + n2 + 1) * sizeof(char)); //After allocating a memory to both sizes of string along with adding memory for NULL.

    for (int i = 0; i < loc; i++)
        *(output + i) = *(p1 + i);

    for (int i = loc - 1; i <= n1; i++)
        *(output + i + n2) = *(p1 + i);

    for (int i = 0; i < n2; i++)
        *(output + i + loc) = *(p2 + i);

    printf("\nFinal copy is: ");
    printf("%s\n", output);

    free(p1);
    free(p2);
    free(output);

    return 0;
}
๐ŸŒ
Quora
quora.com โ€บ How-do-you-properly-dynamically-allocate-a-string-C-string-pointers-memory-management-malloc-development
How to properly dynamically allocate a string (C, string, pointers, memory management, malloc, development) - Quora
Answer (1 of 4): You have 3 choices: malloc, calloc, and realloc. malloc [code ]void *malloc(size_t size);[/code] malloc gives you raw bytes of memory, uninitialized, for any kind of storage you might need. A cast is performed to a pointer type of the needed storage, and the compiler then trea...
๐ŸŒ
Diveintosystems
diveintosystems.org โ€บ book โ€บ C2-C_depth โ€บ strings.html
2.6.1. C's Support for Statically Allocated Strings (Arrays ...
The strcpy function is unsafe to use in situations when the source string might be longer than the total capacity of the destination string. In this case, one should use strncpy. The size parameter stops strncpy from copying more than size characters from the src string into the dst string.
Top answer
1 of 4
30

You could use strdup() to return a copy of a C-string, as in:

#include <string.h>

const char *stringA = "foo";
char *stringB = NULL;

stringB = strdup(stringA);
/* ... */
free(stringB);
stringB = NULL; 

You could also use strcpy(), but you need to allocate space first, which isn't hard to do but can lead to an overflow error, if not done correctly:

#include <string.h>

const char *stringA = "foo";
char *stringB = NULL;

/* you must add one to cover the byte needed for the terminating null character */
stringB = (char *) malloc( strlen(stringA) + 1 ); 
strcpy( stringB, stringA );
/* ... */
free(stringB);
stringB = NULL;

If you cannot use strdup(), I would recommend the use of strncpy() instead of strcpy(). The strncpy() function copies up to โ€” and only up to โ€” n bytes, which helps avoid overflow errors. If strlen(stringA) + 1 > n, however, you would need to terminate stringB, yourself. But, generally, you'll know what sizes you need for things:

#include <string.h>

const char *stringA = "foo";
char *stringB = NULL;

/* you must add one to cover the byte needed for the terminating null character */
stringB = (char *) malloc( strlen(stringA) + 1 ); 
strncpy( stringB, stringA, strlen(stringA) + 1 );
/* ... */
free(stringB);
stringB = NULL;

I think strdup() is cleaner, myself, so I try to use it where working with strings exclusively. I don't know if there are serious downsides to the POSIX/non-POSIX approach, performance-wise, but I am not a C or C++ expert.

Note that I cast the result of malloc() to char *. This is because your question is tagged as a c++ question. In C++, it is required to cast the result from malloc(). In C, however, you would not cast this.

EDIT

There you go, there's one complication: strdup() is not in C or C++. So use strcpy() or strncp() with a pre-sized array or a malloc-ed pointer. It's a good habit to use strncp() instead of strcpy(), wherever you might use that function. It will help reduce the potential for errors.

2 of 4
4

If I just initialize stringB as char *stringB[23], because I know I'll never have a string longer than 22 characters (and allowing for the null terminator), is that the right way?

Almost. In C, if you know for sure that the string will never be too long:

char stringB[MAX+1];
assert(strlen(stringA) <= MAX));
strcpy(stringB, stringA);

or, if there's a possibility that the string might be too long:

char stringB[MAX+1];
strncpy(stringB, stringA, MAX+1);
if (stringB[MAX] != '\0') {
    // ERROR: stringA was too long.
    stringB[MAX] = '\0'; // if you want to use the truncated string
}

In C++, you should use std::string, unless you've proved that the overhead is prohibitive. Many implementations have a "short string optimisation", which will avoid dynamic allocation for short strings; in that case, there will be little or no overhead over using a C-style array. Access to individual characters is just as convenient as with a C-style array; in both cases, s[i] gives the character at position i as an lvalue. Copying becomes stringB = stringA; with no danger of undefined behaviour.

If you really do find that std::string is unusable, consider std::array<char,MAX+1>: a copyable class containing a fixed-size array.

If stringB is checked for equality with other C-strings, will the extra space affect anything?

If you use strcmp, then it will stop at the end of the shortest string, and will not be affected by the extra space.

๐ŸŒ
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'; }
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ c-programming โ€บ 133453-dynamic-memory-allocation-strings.html
Dynamic memory allocation and strings.
January 3, 2011 - char c; char *input = malloc(1); // initial allocation so we can just realloc in our loop int n_char = 0; input[0] = '\0'; // null terminate input in case they enter no data do c = read a character if c is not EOF reallocate n_char+1 bytes into input copy c onto the end of input null terminate input while c is not EOF There is a third method that uses a combination of 1 and 2 so you read in, say, 1000 chars at a time to a buffer and reallocate if need be. This would be my preferred method only if option 1 is not sufficient. ... ... string = realloc(string, bytes_allocated + 2); // + 2 to make room for the input ...