To avoid the buffer overflow that occurs when using the strcpy function, simply replace the strcpy function with the strncpy function. strncpy will copy only the specified number of bytes into the destination string, hence buffer overflows don't occur in this function. Let's see how it is done.
Learning how to write secure code while copying strings
How to do it...
Here are the steps to make secure code while copying a string:
- Define a structure consisting of two members, name and orderid.
- Define a variable of the structure type defined in step 1.
- Assign any integer value to the orderid member of the structure.
- Determine the length of the name member of the structure to find the maximum number of characters...