On April 28, 2009 13:29, in comp.lang.c, Bill Cunningham
() wrote:
> I have this code written like this:
>
> #include <stdio.h>
> #include <sys/stat.h>
>
> int main()
> {
> int i;
> struct stat st;
> if ((i = stat("/bin/e", &st)) == -1) {
> fputs("stat error", stderr);
> return -1;
> }
> printf("%i\n", st.st_blocks);
> printf("%i\n", st.st_blksize);
> return 0;
> }
>
> Now if I wanted instead of
>
> struct stat st;
> struct stat *st;
>
> How would that change the second parameter to stat()? Would it be st
> instead of &st, or *st ?
Hi, Bill
Take a look at the following code, based on your example above...
#include <stdio.h>
#include <sys/stat.h>
int main()
{
int i;
struct stat statstruct;
struct stat *st;
/* Initialize st
** st is a "pointer to struct stat", so
** it takes the address of a struct stat
** to initialize st
*/
st = &statstruct;
/* stat() needs a "pointer to struct stat"
** and that's exactly what st is
*/
if ((i = stat("/bin/e", st)) == -1) {
fputs("stat error", stderr);
return -1;
}
/* st is a "pointer to struct stat"
** so, to access the struct stat members
** through st, we have to dereference the
** pointer. Two methods are shown
*/
printf("%i\n", (*st).st_blocks); /* method 1 - *(st)->member */
printf("%i\n", st->st_blksize); /* method 2 - st->member */
return 0;
}
--
Lew Pitcher
Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------