segmentation fault | | |
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. | | | | re: segmentation fault
Piotr S. wrote:[color=blue]
> 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.[/color]
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);
}
} | | | | re: segmentation fault
Uzytkownik <rrs.matrix@gmail.com> napisal w wiadomosci
news:1150622877.466529.149230@y41g2000cwy.googlegr oups.com...
[color=blue]
> 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)[/color]
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! | | | | re: segmentation fault rrs.matrix@gmail.com wrote:[color=blue]
> Piotr S. wrote:
>[color=green]
>> 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.[/color]
>
> 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)[/color]
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 man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews | | | | re: segmentation fault
On 2006-06-18, Piotr S. <inri@buziaczek.pl> wrote:[color=blue]
> What's wrong in following program:[/color]
Lots.
[color=blue]
> #include <stdio.h>
> #include <stdlib.h>
> char s[80];[/color]
Why are you using a global?
[color=blue]
> int main()[/color]
int main (void) is better.
[color=blue]
> {
> FILE *fp, *fopen();[/color]
*fopen()? Does that really compile unhindered? It doesn't make any sense,
as fopen() is already defined in stdio.h.
[color=blue]
> if(fp = fopen("plik1","r")== NULL)
> {
> printf("blad otwarcia"); exit(1);[/color]
What's wrong with return 1; or better, return EXIT_FAILURE;.
[color=blue]
> }
> else
> {
> printf("\notwarty\n");
> while(fgets(s,30,fp)== NULL)[/color]
While fgets returns a null string, print the null string? That can't be
what you want.
[color=blue]
> printf("%s\n",s);
> }
> }[/color]
[color=blue]
> 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.
>[/color]
I can imagine. You shouldn't try to printf a NULL pointer.
--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
I know that area of town like the back of my head. | | | | re: segmentation fault
Andrew Poelstra wrote:[color=blue]
> On 2006-06-18, Piotr S. <inri@buziaczek.pl> wrote:[color=green]
>> What's wrong in following program:[/color]
> Lots.
>[color=green]
>> #include <stdio.h>
>> #include <stdlib.h>
>> char s[80];[/color]
> Why are you using a global?
>[color=green]
>> int main()[/color]
> int main (void) is better.
>[color=green]
>> {
>> FILE *fp, *fopen();[/color]
> *fopen()? Does that really compile unhindered? It doesn't make any
> sense, as fopen() is already defined in stdio.h.
>[color=green]
>> if(fp = fopen("plik1","r")== NULL)
>> {
>> printf("blad otwarcia"); exit(1);[/color]
> What's wrong with return 1; or better, return EXIT_FAILURE;.
>[color=green]
>> }
>> else
>> {
>> printf("\notwarty\n");
>> while(fgets(s,30,fp)== NULL)[/color]
> While fgets returns a null string, print the null string? That can't
> be what you want.
>[color=green]
>> printf("%s\n",s);
>> }
>> }[/color]
>[color=green]
>> 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.
>>[/color]
>
> I can imagine. You shouldn't try to printf a NULL pointer.[/color]
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?
--
==============
Not a pedant
============== | | | | re: segmentation fault
On Sun, 18 Jun 2006 17:50:26 GMT, Andrew Poelstra
<apoelstra@localhost.localdomain> wrote:
[color=blue]
>On 2006-06-18, Piotr S. <inri@buziaczek.pl> wrote:[color=green]
>> What's wrong in following program:[/color]
>Lots.
>[color=green]
>> #include <stdio.h>
>> #include <stdlib.h>
>> char s[80];[/color]
>Why are you using a global?
>[color=green]
>> int main()[/color]
>int main (void) is better.
>[color=green]
>> {
>> FILE *fp, *fopen();[/color]
>*fopen()? Does that really compile unhindered? It doesn't make any sense,
>as fopen() is already defined in stdio.h.[/color]
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.
[color=blue]
>[color=green]
>> if(fp = fopen("plik1","r")== NULL)
>> {
>> printf("blad otwarcia"); exit(1);[/color]
>What's wrong with return 1; or better, return EXIT_FAILURE;.
>[color=green]
>> }
>> else
>> {
>> printf("\notwarty\n");
>> while(fgets(s,30,fp)== NULL)[/color]
>While fgets returns a null string, print the null string? That can't be
>what you want.[/color]
It's even worse. While fgets returns a NULL pointer, attempting to
print the data pointed to invokes undefined behavior.
[color=blue]
>[color=green]
>> printf("%s\n",s);
>> }
>> }[/color]
>[color=green]
>> 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.
>>[/color]
>
>I can imagine. You shouldn't try to printf a NULL pointer.[/color]
Remove del for email | | | | re: segmentation fault
Piotr S. wrote:
[color=blue]
> With the first statement I was warned by the compiler:
> "assignment to pointer variable without a cast".[/color]
Which is exactly why it is never a smart idea to ignore
compiler warnings ;)
Jussi Jumppanen
Author: Zeus for Windows IDE http://www.zeusedit.com | | | | re: segmentation fault
On Sun, 18 Jun 2006 17:50:26 GMT, Andrew Poelstra
<apoelstra@localhost.localdomain> wrote:
[color=blue]
> On 2006-06-18, Piotr S. <inri@buziaczek.pl> wrote:[/color]
[color=blue][color=green]
> > int main()[/color]
> int main (void) is better.
>[color=green]
> > {
> > FILE *fp, *fopen();[/color]
> *fopen()? Does that really compile unhindered? It doesn't make any sense,
> as fopen() is already defined in stdio.h.
>[/color]
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'.
[color=blue][color=green]
> > printf("%s\n",s);
> > }
> > }[/color]
>[color=green]
> > 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.
> >[/color]
>
> I can imagine. You shouldn't try to printf a NULL pointer.[/color]
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 |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,414 network members.
|