M
Martin Pohlack
Hi,
I have a funtion which shall compute the amount for a later malloc. In
this function I need the sizes of some struct members without having an
instance or pointer of the struct.
As "sizeof(int)" is legal I assumed "sizeof(struct x.y)" to be legal
too. But is is not:
#include <dirent.h>
int dir_dirent_size(dir_t * dirp)
{
int len;
// syntax error
len = sizeof(struct dirent.d_ino) +
sizeof(struct dirent.d_off) +
sizeof(struct dirent.d_reclen) +
strlen(dirp->name) + 1;
return len;
}
I can fix this problem using a dummy pointer to a dirent struct:
#include <dirent.h>
int dir_dirent_size(dir_t * dirp)
{
int len;
struct dirent * temp;
// syntax error
len = sizeof(temp.d_ino) +
sizeof(temp.d_off) +
sizeof(temp.d_reclen) +
strlen(dirp->name) + 1;
return len;
}
However, I'm curious what would be the 'right thing' (tm) to do.
Thank you,
Martin Pohlack
I have a funtion which shall compute the amount for a later malloc. In
this function I need the sizes of some struct members without having an
instance or pointer of the struct.
As "sizeof(int)" is legal I assumed "sizeof(struct x.y)" to be legal
too. But is is not:
#include <dirent.h>
int dir_dirent_size(dir_t * dirp)
{
int len;
// syntax error
len = sizeof(struct dirent.d_ino) +
sizeof(struct dirent.d_off) +
sizeof(struct dirent.d_reclen) +
strlen(dirp->name) + 1;
return len;
}
I can fix this problem using a dummy pointer to a dirent struct:
#include <dirent.h>
int dir_dirent_size(dir_t * dirp)
{
int len;
struct dirent * temp;
// syntax error
len = sizeof(temp.d_ino) +
sizeof(temp.d_off) +
sizeof(temp.d_reclen) +
strlen(dirp->name) + 1;
return len;
}
However, I'm curious what would be the 'right thing' (tm) to do.
Thank you,
Martin Pohlack