Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!ncar!noao!arizona!dave From: dave@cs.arizona.edu (Dave P. Schaumann) Newsgroups: comp.unix.programmer Subject: Re: Segmentation fault? Keywords: error segmentation fault c Message-ID: <945@caslon.cs.arizona.edu> Date: 25 Feb 91 01:43:11 GMT References: <38192@netnews.upenn.edu> Organization: U of Arizona CS Dept, Tucson Lines: 29 Sorry if this appears twice. The first time I tried to follow up, it appeared that nothing actually got posted... In article <38192@netnews.upenn.edu> widyono@eniac.seas.upenn.edu (Aasmodeus) writes: >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); This is it ---------^ line is an array. *line is the first character of that array. When printf sees "%s", it takes whatever the second parameter is, and tries to indirect through it. You can get the same effect by saying char c = 'x' ; *c = 'y' ; A computer that has any kind of memory protection will almost certainly have a segmentation fault at this point. > if (fgets(line, BUFSIZ, fp) == NULL) Here is another, more subtle bug. fgets will write up to BUFSIZ characters (in this call) to line. This is not including the final '\0' character. The best thing to do is declare char line[BUFSIZ + 1], and all will be well. -- Dave Schaumann | Is this question undecidable? dave@cs.arizona.edu | Brought to you by Super Global Mega Corp .com