To enter the name and order number of a customer, define a structure called users with two members, name and orderid. The name member is a character array or string of 10 bytes in length, and the orderid member is a variable of the int data type consisting of 2 bytes.
A variable, user1, is defined as the users structure type; hence, the user1 structure will get two members, name and orderid. An integer value 101 is assigned to the orderid member of user1 structure. Also, a string, administrator, is assigned to the name member of user1. Because the string administrator is larger than the size of the name member, a buffer overflow will occur, overwriting the memory of the next memory location, that is, of the orderid member. Consequently, while displaying the information of the user, though the data in the name member may appear correctly, the content of orderid member will appear incorrectly, as its content is overwritten.
Let's use GCC to compile the strcpyproblem.c program. If you get no errors or warnings, it means the strcpyproblem.c program has compiled into an executable file: strcpyproblem.exe. Let's run this file:
In the preceding output, you can see that because the name member is assigned a string that is larger than its size, this results in it overwriting the content of another member, orderid. The content of the name member appears the same as entered by the user, whereas that of orderid is displayed incorrectly.