Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!wuarchive!udel!princeton!phoenix.Princeton.EDU!subbarao From: subbarao@phoenix.Princeton.EDU (Kartik Subbarao) Newsgroups: comp.unix.programmer Subject: Re: Segmentation fault Keywords: error segmentation fault c Message-ID: <6604@idunno.Princeton.EDU> Date: 25 Feb 91 01:21:28 GMT References: <38191@netnews.upenn.edu> Sender: news@idunno.Princeton.EDU Lines: 34 In article <38191@netnews.upenn.edu> widyono@eniac.seas.upenn.edu (Aasmodeus) writes: > >Hello. I've got a problem that needs a fast solution, fast! Examine the >following bits of code: > >int getline(FILE *fp, char *line); /* decl. should it be line[]? */ >main(int argc, char *argv[]) >{ > char line[BUFSIZ]; /* line of input */ > while (getline(fp, line) > 0) { > printf("%s\n", *line); Here's the problem -- when you have a %s in the format string, printf wants a POINTER to the string, not the first character in it. printf tries to interpret the first character in line as the address of the beginning of the string. Because that first character is in the range 0 thru 128, the program segfaults. >In the case of the above, the error comes in _doprnt. right. just as it should. Another problem may be the fact that you don't assign fp to be anything. -Kartik -- internet# find . -name core -exec cat {} \; |& tee /dev/tty* subbarao@phoenix.Princeton.EDU -| Internet kartik@silvertone.Princeton.EDU (NeXT mail) SUBBARAO@PUCC.BITNET - Bitnet Brought to you by Super Global Mega Corp .com