Why do I have to prefix stat from <sys/stat.h> with the keyword struct?

Discussion in 'C++' started by Rolf Krüger, Mar 12, 2008.

  1. Hi


    I´m about learning C/C++ and after covering the language basics I´m now
    heading for my first "real" application where I need to use the POSIX stuff
    for directory operations.

    Here´s my problem: The following code compiles and runs as it should inside
    a linux g++ environment using code::blocks IDE. But when I drop
    the "struct" prefix from line 12 "struct stat st;" It doesn´t compile any
    more and it says:

    |12|error: expected `;' before ‘st’
    |12|warning: statement is a reference, not call, to function ‘stat’
    |12|warning: statement has no effect
    |18|error: ‘st’ was not declared in this scope

    Hmm, what´s going on here? Why can I drop "struct" from line 1? IMHO dirent
    is just a struct as stat is. A similar behaviour can be found using the
    cygwin environment under windows, so I guess it has nothing to do with
    system dependent header files.


    Any ideas?
    Thanx in advance
    Rolf



    #include <iostream>
    #include <dirent.h>
    #include <sys/stat.h>
    #include <sys/types.h>

    using namespace std;

    int main() {

    dirent *entry;
    struct stat st;

    DIR *base;
    base = opendir(".");

    while( (entry = readdir(base)) != 0) {
    stat(entry->d_name, &st);
    cout << entry->d_name << " :: " << st.st_mode << endl;
    }

    closedir(base);

    return 0;
    }
     
    Rolf Krüger, Mar 12, 2008
    #1
    1. Advertising

  2. Hi Ian,

    >> Here´s my problem: The following code compiles and runs as it should
    >> inside a linux g++ environment using code::blocks IDE. But when I drop
    >> the "struct" prefix from line 12 "struct stat st;" It doesn´t compile any
    >> more and it says:
    >>
    >> |12|error: expected `;' before ‘st’
    >> |12|warning: statement is a reference, not call, to function ‘stat’
    >> |12|warning: statement has no effect
    >> |18|error: ‘st’ was not declared in this scope


    > stat is both a struct and a function. Very confusing, but simple to
    > disambiguate with 'struct stat'.


    Thank you very much

    I´ve taken more than one close look into the sys/stat.h file ... obviously
    not enough - should have seen this by myself!

    Thanks
    Rolf
     
    Rolf Krüger, Mar 12, 2008
    #2
    1. Advertising

  3. Rolf Krüger

    Ian Collins Guest

    Re: Why do I have to prefix stat from <sys/stat.h> with the keywordstruct?

    Rolf Krüger wrote:
    > Hi
    >
    >
    > I´m about learning C/C++ and after covering the language basics I´m now
    > heading for my first "real" application where I need to use the POSIX stuff
    > for directory operations.
    >
    > Here´s my problem: The following code compiles and runs as it should inside
    > a linux g++ environment using code::blocks IDE. But when I drop
    > the "struct" prefix from line 12 "struct stat st;" It doesn´t compile any
    > more and it says:
    >
    > |12|error: expected `;' before ‘st’
    > |12|warning: statement is a reference, not call, to function ‘stat’
    > |12|warning: statement has no effect
    > |18|error: ‘st’ was not declared in this scope
    >

    stat is both a struct and a function. Very confusing, but simple to
    disambiguate with 'struct stat'.

    --
    Ian Collins.
     
    Ian Collins, Mar 12, 2008
    #3
    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. Tim Tyler
    Replies:
    36
    Views:
    1,527
    Darryl L. Pierce
    Dec 10, 2004
  2. Steven T. Hatton
    Replies:
    3
    Views:
    9,760
    Pete Becker
    Jun 2, 2005
  3. Holger Joukl
    Replies:
    1
    Views:
    467
    Michael Hudson
    Feb 19, 2004
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,220
    Smokey Grindel
    Dec 2, 2006
  5. Magesh
    Replies:
    3
    Views:
    465
    Gordon Burditt
    Oct 5, 2007
Loading...

Share This Page