How to compare nlink_t to int
c, file, parsing, variables
Solution
`nlink_t` is typedef'd as an integer type (e.g., `unsigned short` or `unsigned int`), so you should be able to cast `stat.st_nlink` to an `unsigned` or `unsigned long` without the compiler complaining.
Your `parse_to_int()` function is incorrect, because you're casting a pointer (`nlink_t*`) to an `unsigned int`, instead of the value of the `nlink_t` variable. But you don't need the function, just use the cast properly.
Addendum
Also make sure that you're not comparing an unsigned type to `-1`, which will give you unexpected results.
Problem
I use `stat` system call on Linux and retrieve file information. ``` char *parent_dir; // for example: /run/atd.pid/ struct stat buf; stat(parent_dir, &buf); ``` buf structure type: ``` struct stat { dev_t st_dev; /* ID of device containing file */ ino_t st_ino; /* inode number */ mode_t st_mode; /* protection */ nlink_t st_nlink; /* number of hard links */ uid_t st_uid; /* user ID of owner */ gid_t st_gid; /* group ID of owner */ dev_t st_rdev; /* device ID (if special file) */ off_t st_size; /* total size, in bytes */ blksize_t st_blksize; /* blocksize for file system I/O */ blkcnt_t st_blocks; /* number of 512B blocks allocated */ time_t st_atime; /* time of last access */ time_t st_mtime; /* time of last modification */ time_t st_ctime; /* time of last status change */ }; ``` I get number of hard links like this: `buf.st_nlink`. My problem is that I can't compare number of hard links to integer value. I've tried to initialize another nlink_t and then compare my variable to `stat` variable, but it doesn't work. I've also tried this link. Alternative way to cast nlink_t to int, but it doesn't work. always returns the same number. ``` int parse_to_int(nlink_t *source) { int buffer_size = sizeof(*source); char buffer[buffer_size]; snprintf(&buffer[0], buffer_size, "%lu", (unsigned long)source); int val = atoi(buffer); return val; } ``` Any idea? Program output when I use `parse_to_int` function: ``` get stat for: /run/nm-dhclient-wlan0.conf/ nlink_t: 321 get stat for: /run/wpa_supplicant/ nlink_t: 321 get stat for: /run/udisks2/ nlink_t: 321 get stat for: /run/nm-dns-dnsmasq.conf/ nlink_t: 321 ... ```