Because C does not enforce/check your array size. you get 10 bytes of memory for your array, sure, but when you write, it writes until it hits the \0 character. So, when you type more than 10 characters, you are over-writing other memory that isn't yours. This is a common way to cause core dumps in C - you overwrite memory you're not supposed to, which hoses the program.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
ummm...they way you did it. They just had to be careful to define a large enough buffer to handle the expected input.
You could do things like create one large buffer you use for all your inputs, then manually verify it... i.e.
//read name into buffer
//copy first 9 chars into Name string, and set 10th to null
//read address into buffer
//copy first 29 chars into buffer and set 30th to null
//read city into buffer
A common way to fix bugs in C code was to simply double all your declarations, on the assumption that your probably overwriting memory somewhere, so by doubling everything, you will now be less likely to blow away memory that isn't yours.
why aint it dispalying the proper out despite oweriting memory???
and sometimes looping duch programss just to input multiple name creates a strange out put
Joined: Oct 13, 2005
You are trying to write the memory into the location described by the contents of the 24th element of that char array. That is likely to be a bit of memory far from the location of your program, which may give a seg fault. Try name + 23 instead.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com