Memor fault, Core dump

Wolfram Roesler wolfram at cip-s01.informatik.rwth-aachen.de
Wed Jun 5 21:21:38 AEST 1991


nnj20229 at uxa.cso.uiuc.edu (Nesha Nicole Jones) writes:

>I am having a small problem debugging a program of mine. I get a memory
>fault after a fprintf statement.

First idea: did you pass the FILE* as the first arg to fprintf? This is
usually the reason for me...

>I was trying to allow the user to input 
>more than one line of information at a prompt. 

Ha! Just imagine the user inputs a line like: "hello you %s", then
you will have `fprintf(fp,"hello you %s");'. fprintf will then encounter
the %s, take something from its vararg list, assume it's a pointer and
BOUMMMMM! To print something the user has entered (say, in a string named St),
NEVER say `fprintf(fp,St)' but use `fprintf(fp,"%s",St)' or even better
`fputs(St,fp)'.
You should use [sf]printf only if you actually want to make use of the format
elements introduced by '%'. The same problem is `sprintf(s1,s2)', use
strcpy instead.

>also, I read in two values, a date and a time.  I strcat the date and time
>together and the value for date is somehow changed to the 5th char in time
>ex.  09:45   date= 5

Please explain that more precisely.

>Any suggtestions about why this happens would also be helpfull.

I hope so.

BCingU
\/\/olfram



More information about the Comp.lang.c mailing list