A short introduction into the magic of pointers:

char *strings[10],string[50],*p;

These are three variables with distinct types:

char *strings[10]; // an array of 10 pointers to char
char string[50]; // an array of 50 char
char *p; // a pointer to char

Then the followin is done (10 times):

    scanf(" %49[^\n]",string);

Read C string from input and store it into string considering that a 0 terminator must fit in also.

    length = strlen(string);

Count non-0 characters until 0 terminator is found and store in length.

    p = (char *)malloc(length+1);

Allocate memory on heap with length + 1 (for 0 terminator) and store address of that memory in p. (malloc() might fail. A check if (p != NULL) wouldn't hurt.)

    strcpy(p,string);//why use strcpy here instead of p = string

Copy C string in string to memory pointed in p. strcpy() copies until (inclusive) 0 terminator is found in source.

    strings[i] = p;

Assign p (the pointer to memory) to strings[i]. (After assignment strings[i] points to the same memory than p. The assignment is a pointer assignment but not the assignment of the value to which is pointed.)


Why strcpy(p,string); instead of p = string:

The latter would assign address of string (the local variable, probably stored on stack) to p.

  1. The address of allocated memory (with malloc()) would have been lost. (This introduces a memory leak - memory in heap which cannot be addressed by any pointer in code.)

  2. p would now point to the local variable in string (for every iteration in for loop). Hence afterwards, all entries of strings[10] would point to string finally.

Answer from Scheff's Cat on Stack Overflow
🌐
Cplusplus
cplusplus.com › reference › cstring › strcpy
strcpy - cstring
strcpy · strcspn · strerror · strlen · strncat · strncmp · strncpy · strpbrk · strrchr · strspn · strstr · strtok · strxfrm · size_t · NULL · Reference · <cstring> strcpy · function · <cstring> char * strcpy ( char * destination, const char * source ); Copy string ·
🌐
GeeksforGeeks
geeksforgeeks.org › c language › strcpy-in-c
strcpy() in C - GeeksforGeeks
March 6, 2026 - In C, strcpy() is a built-in function used to copy one string into another.
People also ask

What is SCRCPY?
SCRCPY is a free and open-source tool that allows you to view and control your Android device from your computer. It uses a USB connection to mirror your device's screen to your computer.
🌐
scrcpy.org
scrcpy.org
SCRCPY – Android Screen Mirroring Software | Free Download (Latest)
What are the system requirements for SCRCPY?
SCRCPY requires a Windows PC, macOS, or Linux system and an Android phone. There are no specific system requirements, but a reasonably powerful PC and a phone with good screen resolution are recommended.
🌐
scrcpy.org
scrcpy.org
SCRCPY – Android Screen Mirroring Software | Free Download (Latest)
Is SCRCPY free?
Yes, SCRCPY is completely free and open source. The source code for SCRCPY is available to the public.
🌐
scrcpy.org
scrcpy.org
SCRCPY – Android Screen Mirroring Software | Free Download (Latest)
🌐
Linux Man Pages
man7.org › linux › man-pages › man3 › strcpy.3.html
strcpy(3) - Linux manual page
stpcpy() strcpy() These functions copy the string pointed to by src, into a string at the buffer pointed to by dst. The programmer is responsible for allocating a destination buffer large enough, that is, strlen(src) + 1. For the difference between the two functions, see RETURN VALUE.
🌐
University of Toronto
cs.toronto.edu › ~guerzhoy › 190 › calendar.html
ESC190: Computer Algorithms and Data Structures (Calendar)
Slides 1 (Weeks 1–4) | Slides 2 (Weeks 5+) LecturesReading and Materials Week 1 · Lec 1 (Jan 5): First C program, printf, variables, scanf, compilation with gcc
🌐
Blender
blender.org › download › lts › 3-6
Blender 3.6 LTS — Blender
Fix potential buffer overflow in strcpy use on macOS.
🌐
SCRCPY
scrcpy.org
SCRCPY – Android Screen Mirroring Software | Free Download (Latest)
Download SCRCPY for free. Mirror and control your Android device from your PC with low latency. Open-source screen mirroring tool for Windows, macOS, and Linux.
Find elsewhere
🌐
Quora
quora.com › What-does-Strcpy-do-in-C
What does Strcpy do in C? - Quora
Answer: If you have defined a function Strcpy() somewhere in your code, it executes that function. The function Strcpy(), with a capital S, does not exist in the C standard library. If you meant to ask about strcpy() with a lowercase s, it acts exactly like the following C code: [code]char *strc...
🌐
Cppreference
en.cppreference.com › w › cpp › string › byte › strcpy.html
std::strcpy - cppreference.com
#include <cstring> #include <iostream> #include <memory> int main() { const char* src = "Take the test."; // src[0] = 'M'; // can't modify string literal auto dst = std::make_unique<char[]>(std::strlen(src) + 1); // +1 for null terminator std::strcpy(dst.get(), src); dst[0] = 'M'; std::cout << src << '\n' << dst.get() << '\n'; }
🌐
The Open Group
pubs.opengroup.org › onlinepubs › 7908799 › xsh › strcpy.html
strcpy
The strcpy() function copies the string pointed to by s2 (including the terminating null byte) into the array pointed to by s1.
Top answer
1 of 3
1

A short introduction into the magic of pointers:

char *strings[10],string[50],*p;

These are three variables with distinct types:

char *strings[10]; // an array of 10 pointers to char
char string[50]; // an array of 50 char
char *p; // a pointer to char

Then the followin is done (10 times):

    scanf(" %49[^\n]",string);

Read C string from input and store it into string considering that a 0 terminator must fit in also.

    length = strlen(string);

Count non-0 characters until 0 terminator is found and store in length.

    p = (char *)malloc(length+1);

Allocate memory on heap with length + 1 (for 0 terminator) and store address of that memory in p. (malloc() might fail. A check if (p != NULL) wouldn't hurt.)

    strcpy(p,string);//why use strcpy here instead of p = string

Copy C string in string to memory pointed in p. strcpy() copies until (inclusive) 0 terminator is found in source.

    strings[i] = p;

Assign p (the pointer to memory) to strings[i]. (After assignment strings[i] points to the same memory than p. The assignment is a pointer assignment but not the assignment of the value to which is pointed.)


Why strcpy(p,string); instead of p = string:

The latter would assign address of string (the local variable, probably stored on stack) to p.

  1. The address of allocated memory (with malloc()) would have been lost. (This introduces a memory leak - memory in heap which cannot be addressed by any pointer in code.)

  2. p would now point to the local variable in string (for every iteration in for loop). Hence afterwards, all entries of strings[10] would point to string finally.

2 of 3
1
char *strings[10]---- --------->1.
strcpy(strings[i],string) ----->2.
strings[i] = string ----------->3.


p = (char *)malloc(length+1); -|
strcpy(p,string);              |-> 4.
strings[i] = p;----------------|
  1. strings is an array of pointers, each pointer must point to valid memory.

  2. Will lead undefined behavior since strings[i] is not pointing to valid memory.

  3. Works but every pointer of strings will point to same location thus each will have same contents.
  4. Thus create the new memory first, copy the contents to it and assign that memory to strings[i]
🌐
Codidact
software.codidact.com › posts › 281518
Is strcpy dangerous and what should be used instead? - Software Development
So if the application programmer just merrily strcpy some provided argv command line argument into a 100 bytes large stack-allocated buffer, and there's a return address sitting on the stack 5 bytes further down, then the hacker would provide those extra bytes to overwrite that address.
Top answer
1 of 8
17

You've just caused undefined behaviour, so anything can happen. In your case, you're getting lucky and it's not crashing, but you shouldn't rely on that happening. Here's a simplified strcpy implementation (but it's not too far off from many real ones):

char *strcpy(char *d, const char *s)
{
   char *saved = d;
   while (*s)
   {
       *d++ = *s++;
   }
   *d = 0;
   return saved;
}

sizeof is just returning you the size of your array from compile time. If you use strlen, I think you'll see what you expect. But as I mentioned above, relying on undefined behaviour is a bad idea.

2 of 8
5

http://natashenka.ca/wp-content/uploads/2014/01/strcpy8x11.png

strcpy is considered dangerous for reasons like the one you are demonstrating. The two buffers you created are local variables stored in the stack frame of the function. Here is roughly what the stack frame looks like: http://upload.wikimedia.org/wikipedia/commons/thumb/d/d3/Call_stack_layout.svg/342px-Call_stack_layout.svg.png

FYI things are put on top of the stack meaning it grows backwards through memory (This does not mean the variables in memory are read backwards, just that newer ones are put 'behind' older ones). So that means if you write far enough into the locals section of your function's stack frame, you will write forward over every other stack variable after the variable you are copying to and break into other sections, and eventually overwrite the return pointer. The result is that if you are clever, you have full control of where the function returns. You could make it do anything really, but it isn't YOU that is the concern.

As you seem to know by making your first buffer 6 chars long for a 5 character string, C strings end in a null byte \x00. The strcpy function copies bytes until the source byte is 0, but it does not check that the destination is that long, which is why it can copy over the boundary of the array. This is also why your print is reading the buffer past its size, it reads till \x00. Interestingly, the strcpy may have written into the data of s depending on the order the compiler gave it in the stack, so a fun exercise could be to also print a and see if you get something like 'snsadsdas', but I can't be sure what it would look like even if it is polluting s because there are sometimes bytes in between the stack entries for various reasons).

If this buffer holds say, a password to check in code with a hashing function, and you copy it to a buffer in the stack from wherever you get it (a network packet if a server, or a text box, etc) you very well may copy more data from the source than the destination buffer can hold and give return control of your program to whatever user was able to send a packet to you or try a password. They just have to type the right number of characters, and then the correct characters that represent an address to somewhere in ram to jump to.

You can use strcpy if you check the bounds and maybe trim the source string, but it is considered bad practice. There are more modern functions that take a max length like http://www.cplusplus.com/reference/cstring/strncpy/

Oh and lastly, this is all called a buffer overflow. Some compilers add a nice little blob of bytes randomly chosen by the OS before and after every stack entry. After every copy the OS checks these bytes against its copy and terminates the program if they differ. This solves a lot of security problems, but it is still possible to copy bytes far enough into the stack to overwrite the pointer to the function to handle what happens when those bytes have been changed thus letting you do the same thing. It just becomes a lot harder to do right.

🌐
Linux Man Pages
linux.die.net › man › 3 › strcpy
strcpy(3): copy string - Linux man page
The strcpy() function copies the string pointed to by src, including the terminating null byte ('\0'), to the buffer pointed to by dest.
🌐
Tenable
tenable.com › cve › CVE-2026-5566
CVE-2026-5566<!-- --> | Tenable®
2 weeks ago - A vulnerability was detected in UTT HiPER 1250GW up to 3.2.7-210907-180535. This affects the function strcpy of the file /goform/formNatStaticMap. Performing a manipulation of the argument NatBind results in buffer overflow. Remote exploitation of the attack is possible.
🌐
Tutorial Gateway
tutorialgateway.org › strcpy-in-c-programming
C strcpy Function
3 weeks ago - The built-in C strcpy function is useful to copy a user-specified string or content (a group of characters) from one string to another.
🌐
Wikibooks
en.wikibooks.org › wiki › C_Programming › string.h › strcpy
C Programming/string.h/strcpy - Wikibooks, open books for an open world
The strcpy function performs a copy by iterating over the individual characters of the string and copying them one by one.
🌐
Programiz
programiz.com › c-programming › library-function › string.h › strcpy
C strcpy() - C Standard Library
The strcpy() function copies the string pointed by source (including the null character) to the destination.
🌐
Sternum IoT
sternumiot.com › home › strcpy and strncpy c functions – syntax, examples, and security best practices
strcpy and strncpy C Functions | Syntax, Examples & Security Best Practices | Sternum IoT
January 30, 2024 - The strcpy() function works by taking two arguments: a pointer to the destination buffer (called dest) and a pointer to the source string (called src). The function iterates through the characters in the source string, copying each character to the destination buffer, and finally appending a null character \0 to terminate the destination string.
🌐
W3Schools
w3schools.com › c › ref_string_strcpy.php
C string strcpy() Function
The strcpy() function copies data from one string into the memory of another string.