segmentation fault

Discussion in 'C Programming' started by Piotr S., Jun 18, 2006.

  1. Piotr S.

    Piotr S. Guest

    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.
     
    Piotr S., Jun 18, 2006
    #1
    1. Advertising

  2. Piotr S.

    Guest

    Piotr S. wrote:
    > 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);
    }

    }
     
    , Jun 18, 2006
    #2
    1. Advertising

  3. Piotr S.

    Piotr S. Guest

    Uzytkownik <> napisal w wiadomosci
    news:...


    > 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!
     
    Piotr S., Jun 18, 2006
    #3
  4. Piotr S.

    CBFalconer Guest

    wrote:
    > Piotr S. wrote:
    >
    >> 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 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
     
    CBFalconer, Jun 18, 2006
    #4
  5. On 2006-06-18, Piotr S. <> wrote:
    > 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.

    --
    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.
     
    Andrew Poelstra, Jun 18, 2006
    #5
  6. Piotr S.

    pemo Guest

    Andrew Poelstra wrote:
    > On 2006-06-18, Piotr S. <> wrote:
    >> 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?


    --
    ==============
    Not a pedant
    ==============
     
    pemo, Jun 18, 2006
    #6
  7. On Sun, 18 Jun 2006 17:50:26 GMT, Andrew Poelstra
    <> wrote:

    >On 2006-06-18, Piotr S. <> wrote:
    >> 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.

    >
    >> 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.


    It's even worse. While fgets returns a NULL pointer, attempting to
    print the data pointed to invokes undefined behavior.

    >
    >> 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.



    Remove del for email
     
    Barry Schwarz, Jun 19, 2006
    #7
  8. Piotr S.

    Guest

    Piotr S. wrote:

    > With the first statement I was warned by the compiler:
    > "assignment to pointer variable without a cast".


    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
     
    , Jun 19, 2006
    #8
  9. On Sun, 18 Jun 2006 17:50:26 GMT, Andrew Poelstra
    <> wrote:

    > On 2006-06-18, Piotr S. <> wrote:


    > > 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.
    >

    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'.

    > > 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.


    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
     
    Dave Thompson, Jun 26, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Hunsley
    Replies:
    17
    Views:
    893
  2. Pud
    Replies:
    0
    Views:
    605
  3. Replies:
    0
    Views:
    570
  4. Ivan Vecerina
    Replies:
    0
    Views:
    506
    Ivan Vecerina
    Jun 29, 2003
  5. Vasileios Zografos

    Re: segmentation fault exception handling

    Vasileios Zografos, Jun 30, 2003, in forum: C++
    Replies:
    5
    Views:
    15,744
    Pete Becker
    Jul 1, 2003
Loading...

Share This Page