segmentation fault

P

Piotr S.

What's wrong in following program:
#include <stdio.h>
#include <stdlib.h>
char s[80];
int main()
{
FILE *fp, *fopen();
if(fp = fopen("plik1","r")== NULL)
{
printf("blad otwarcia"); exit(1);
}
else
{
printf("\notwarty\n");
while(fgets(s,30,fp)== NULL)
printf("%s\n",s);
}
}
I thought that buffor s is not properly declared, I had tried to declare
this variable
inside main(), and as char * s; Everytime runnig the program resulted in
segmentation fault.
 
R

rrs.matrix

Piotr said:
What's wrong in following program:
#include <stdio.h>
#include <stdlib.h>
char s[80];
int main()
{
FILE *fp, *fopen();
if(fp = fopen("plik1","r")== NULL)
{
printf("blad otwarcia"); exit(1);
}
else
{
printf("\notwarty\n");
while(fgets(s,30,fp)== NULL)
printf("%s\n",s);
}
}
I thought that buffor s is not properly declared, I had tried to declare
this variable
inside main(), and as char * s; Everytime runnig the program resulted in
segmentation fault.


if(fp = fopen("plik1","r")== NULL)
this statement is wrong. it should be
if((fp = fopen("plik1","r"))== NULL)

while(fgets(s,30,fp)== NULL)
this statement also is wrong.
it should be
while(fgets(s,30,fp)!= NULL)

the changed program would be.
#include <stdio.h>
#include <stdlib.h>
char s[80];
int main()
{
FILE *fp, *fopen();
if((fp = fopen("plik1","r"))== NULL)
{
printf("blad otwarcia"); exit(1);
}
else
{
printf("\notwarty\n");
while(fgets(s,30,fp)!= NULL)
printf("%s\n",s);
}

}
 
P

Piotr S.

Uzytkownik <[email protected]> napisal w wiadomosci

if(fp = fopen("plik1","r")== NULL)
this statement is wrong. it should be
if((fp = fopen("plik1","r"))== NULL)

while(fgets(s,30,fp)== NULL)
this statement also is wrong.
it should be
while(fgets(s,30,fp)!= NULL)
Of course the last statement was logically wrong - would produce a not
expected output. With the first statement I was warned by the compiler:
"assignment to pointer variable without a cast". Thanks, it's working now!
 
C

CBFalconer

Piotr said:
What's wrong in following program:
#include <stdio.h>
#include <stdlib.h>
char s[80];
int main()
{
FILE *fp, *fopen();
if(fp = fopen("plik1","r")== NULL)
{
printf("blad otwarcia"); exit(1);
}
else
{
printf("\notwarty\n");
while(fgets(s,30,fp)== NULL)
printf("%s\n",s);
}
}
I thought that buffor s is not properly declared, I had tried to
declare this variable inside main(), and as char * s; Everytime
runnig the program resulted in segmentation fault.

if(fp = fopen("plik1","r")== NULL)
this statement is wrong. it should be
if((fp = fopen("plik1","r"))== NULL)

while(fgets(s,30,fp)== NULL)
this statement also is wrong.
it should be
while(fgets(s,30,fp)!= NULL)

I actually stared at the original without spotting that fault, only
the silly redeclaration of *fopen(). Once more the advantage of
writing comparisons to constants as:

if (NULL == (fp = fopen(...))) {

appears. With that construct it is hard to omit the parens.

However the compiler should have complained about the assignment of
an integer to a pointer anyhow.
 
A

Andrew Poelstra

What's wrong in following program: Lots.

#include <stdio.h>
#include <stdlib.h>
char s[80];
Why are you using a global?
int main()
int main (void) is better.
{
FILE *fp, *fopen();
*fopen()? Does that really compile unhindered? It doesn't make any sense,
as fopen() is already defined in stdio.h.
if(fp = fopen("plik1","r")== NULL)
{
printf("blad otwarcia"); exit(1);
What's wrong with return 1; or better, return EXIT_FAILURE;.
}
else
{
printf("\notwarty\n");
while(fgets(s,30,fp)== NULL)
While fgets returns a null string, print the null string? That can't be
what you want.
printf("%s\n",s);
}
}
I thought that buffor s is not properly declared, I had tried to declare
this variable
inside main(), and as char * s; Everytime runnig the program resulted in
segmentation fault.

I can imagine. You shouldn't try to printf a NULL pointer.
 
P

pemo

Andrew said:
What's wrong in following program: Lots.

#include <stdio.h>
#include <stdlib.h>
char s[80];
Why are you using a global?
int main()
int main (void) is better.
{
FILE *fp, *fopen();
*fopen()? Does that really compile unhindered? It doesn't make any
sense, as fopen() is already defined in stdio.h.
if(fp = fopen("plik1","r")== NULL)
{
printf("blad otwarcia"); exit(1);
What's wrong with return 1; or better, return EXIT_FAILURE;.
}
else
{
printf("\notwarty\n");
while(fgets(s,30,fp)== NULL)
While fgets returns a null string, print the null string? That can't
be what you want.
printf("%s\n",s);
}
}
I thought that buffor s is not properly declared, I had tried to
declare this variable
inside main(), and as char * s; Everytime runnig the program
resulted in segmentation fault.

I can imagine. You shouldn't try to printf a NULL pointer.

As he doesn't mention static, and shows no initialiser, I suspect it was a
random pointer he was writing to.

'notwarty' - love it - what language is this?
 
B

Barry Schwarz

What's wrong in following program: Lots.

#include <stdio.h>
#include <stdlib.h>
char s[80];
Why are you using a global?
int main()
int main (void) is better.
{
FILE *fp, *fopen();
*fopen()? Does that really compile unhindered? It doesn't make any sense,
as fopen() is already defined in stdio.h.

It is only declared in stdio.h. Since this declaration is at
function/block scope, it serves to temporarily replace the declaration
in stdio.h only for code in this function/block.
What's wrong with return 1; or better, return EXIT_FAILURE;.

While fgets returns a null string, print the null string? That can't be
what you want.

It's even worse. While fgets returns a NULL pointer, attempting to
print the data pointed to invokes undefined behavior.
I can imagine. You shouldn't try to printf a NULL pointer.


Remove del for email
 
D

Dave Thompson

int main (void) is better.

*fopen()? Does that really compile unhindered? It doesn't make any sense,
as fopen() is already defined in stdio.h.
Yes. It is a redeclaration of fopen with a type (function of
unspecified arguments returning pointer to FILE) that is compatible
with, though less specific than, the type specified by stdio.h
(function of two [in C99 restricted] pointers to const char returning
as above); redeclaring functions and for that matter nonauto objects
in this fashion is legal and no diagnostic is required, although
redeclaring standard functions anywhere is generally silly.

Nits: stdio.h declares but not defines it. And since standard headers
need not be actual files, I prefer to say 'by' rather than 'in'.
I can imagine. You shouldn't try to printf a NULL pointer.

Not *printf as a string %s as here; nor an invalid (indeterminate)
pointer which it will be if declared 'inside main()' hence at block
scope and automatic, and without an initializer. Nor use it as a
pointer in any other way, like in the preceding (snipped) fgets().

You _can_ *printf the pointer itself if valid including null with %p.

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top