this prog. is supposed to demonstrate a strcpy error...however im getting an error and it wont run....once again im suing a dev c++ bloodshed compiler.
and heres my code....once again i know it has a strcpy error thats the reason for writting the program..to demonstrate a char array overflow.


#include <iostream>
#include <string>

int main()
    char student_name[21] = "Brandon Adkins"; //declare four
    char city[] = "Lubbock";
    char state[3] = "TX";
    char ZIP_code[11] = "79413-3640";

    cout << student_name << '\n'; //display the strings
    cout << city << '\n';
    cout << state << '\n';
    cout << ZIP_code << '\n';

    strcpy(state,"California"); //copy a string that is too long
                                //into the state char. array.

    cout << student_name << '\n'; //display the strings again
    cout << city << '\n';         //showing the overwritten array.
    cout << state << '\n';
    cout << ZIP_code << '\n';

    return 0;

so wiser ones....whats the problem now....
(side not....dev compiler is sooo annoying...i messed around with a visual studios compiler and it was more user friendly....)
im going out of a book here but unfortunately it doesnt support devs picky code. it doesnt say what dev needs extra for the code i write...like the cin.get(); command..these things are not listed..sorry to waste time...peace

Edited by pyTony: fixed formating

12 Years
Discussion Span
Last Post by Narue

Actually quite an interesting code. After the correction the code will compile. BTW in this case you only need one cin.get() for the console to wait, since there is no enter to trap.

At least on my machine there is no effect after using just
or even
but after
the extra long state string starts overwriting the city and if you use four Californias in the state string it will hit the name too.


hahaha...your right, we all make mistakes sometimes...jeez, im stupid.

That didn't translate very well. What does your bee do exactly?


>to demonstrate a char array overflow.
Array overflow is undefined. Your demonstration is non-sensical because the result could be anything.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.