Subj : Re: General Protection Fault To : borland.public.cpp.borlandcpp From : Ed Mulroy [TeamB] Date : Sun Jan 23 2005 11:00 am Use a function which will respect the size of the buffer. While you are at it change the return type of main to int so the code is be legal C/C++. #include int main() { char name[50]; printf("Hello World!\n"); printf("Enter your name...\n") ; fgets(name, sizeof(name), stdin); printf("Hello %s\n",name) ; return 0; } .. Ed > Todd Burch wrote in message > news:41f3a38c$1@newsgroups.borland.com... > > I can cause a general protection fault quite easily, and > I know exactly why, but I don't know the correct technique > to prevent getting one. > > Here is the code: > > #include > void main() > { > char name[50] ; > printf("Hello World!\n"); > printf("Enter your name...\n") ; > gets(name); > printf("Hello %s\n",name) ; > } > > When I enter a name quite a bit longer than 50 bytes, I get > the fault. How does one code in C/C++ to make sure this > is not allowed > to happen? > > I suspect it comes down to working with pointers... and > using a lower level interface than gets(). .