【发布时间】:2022-02-15 05:32:21
【问题描述】:
我看到this 帖子讨论如何从stat 库打印文件权限,但我对结构的定义如何工作感到困惑。
当我定义结构时,我总是将它们定义为struct struct_type *name。但在示例中,结构定义为struct struct_type name(不是指针)。据我所知,应该有两种不同的方式来访问信息,具体取决于您定义结构的方式。
选项一表示法
struct stat fileStat; //definition of struct
stat("<filename>", &fileStat); //execute the stat function
fileStat.member_name; //access a member of a struct
选项二表示法
struct stat *fileStat; //definition of struct
stat("<filename>", fileStat); //execute the stat function
fileStat->member_name; //access a member of a struct
但是当我从提供的示例中的选项一符号切换到选项二时,我得到了非常不同的结果。这两个选项有什么不同?它不是在有效地做同样的事情吗?
示例代码
选项一符号程序
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
int main(int argc, char **argv)
{
struct stat fileStat;
stat("main", &fileStat);
printf("Information for %s\n", argv[1]);
printf("---------------------------\n");
printf("File Size: \t\t%lld bytes\n", fileStat.st_size);
printf("Number of Links: \t%d\n", fileStat.st_nlink);
printf("File inode: \t\t%llu\n", fileStat.st_ino);
printf("File Permissions: \t");
printf( (S_ISDIR(fileStat.st_mode)) ? "d" : "-");
printf( (fileStat.st_mode & S_IRUSR) ? "r" : "-");
printf( (fileStat.st_mode & S_IWUSR) ? "w" : "-");
printf( (fileStat.st_mode & S_IXUSR) ? "x" : "-");
printf( (fileStat.st_mode & S_IRGRP) ? "r" : "-");
printf( (fileStat.st_mode & S_IWGRP) ? "w" : "-");
printf( (fileStat.st_mode & S_IXGRP) ? "x" : "-");
printf( (fileStat.st_mode & S_IROTH) ? "r" : "-");
printf( (fileStat.st_mode & S_IWOTH) ? "w" : "-");
printf( (fileStat.st_mode & S_IXOTH) ? "x" : "-");
printf("\n\n");
printf("The file %s a symbolic link\n", (S_ISLNK(fileStat.st_mode)) ? "is" : "is not");
return 0;
}
选项一符号输出
Information for (null)
---------------------------
File Size: 49960 bytes
Number of Links: 1
File inode: 58527531
File Permissions: -rwxr-xr-x
The file is not a symbolic link
选项二符号程序
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
int main(int argc, char **argv)
{
struct stat *fileStat;
stat("main", fileStat);
printf("Information for %s\n", argv[1]);
printf("---------------------------\n");
printf("File Size: \t\t%lld bytes\n", fileStat->st_size);
printf("Number of Links: \t%d\n", fileStat->st_nlink);
printf("File inode: \t\t%llu\n", fileStat->st_ino);
printf("File Permissions: \t");
printf( (S_ISDIR(fileStat->st_mode)) ? "d" : "-");
printf( (fileStat->st_mode & S_IRUSR) ? "r" : "-");
printf( (fileStat->st_mode & S_IWUSR) ? "w" : "-");
printf( (fileStat->st_mode & S_IXUSR) ? "x" : "-");
printf( (fileStat->st_mode & S_IRGRP) ? "r" : "-");
printf( (fileStat->st_mode & S_IWGRP) ? "w" : "-");
printf( (fileStat->st_mode & S_IXGRP) ? "x" : "-");
printf( (fileStat->st_mode & S_IROTH) ? "r" : "-");
printf( (fileStat->st_mode & S_IWOTH) ? "w" : "-");
printf( (fileStat->st_mode & S_IXOTH) ? "x" : "-");
printf("\n\n");
printf("The file %s a symbolic link\n", (S_ISLNK(fileStat->st_mode)) ? "is" : "is not");
return 0;
}
选项二符号输出
Information for (null)
---------------------------
File Size: 5193343115435036343 bytes
Number of Links: 12487
File inode: 930377443599221576
File Permissions: -r-x--x---
The file is not a symbolic link
【问题讨论】:
-
您的“选项 2”显然是错误的。
fileStat是未初始化的指针。 -
在选项二中,您没有为
stat结构分配空间。 -
这与
stat无关。它只是基本的 C。struct stat *fileStat;是一个未初始化的指针。访问任何未初始化的指针都会导致未定义的行为。将其与&fileStat进行比较,后者也是一个指针,但它指向一个实际有效的内存块(fileStat变量)。
标签: c pointers struct linked-list