By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,932 Members | 1,462 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,932 IT Pros & Developers. It's quick & easy.

sizeof operator question

P: n/a

Hello Everyone,

I've ran into a little snag with the sizeof operator. Can you get the
size of a member inside a struct using sizeof? I looked through the FAQ
and I couldn't find the answer to this one.

Below is the code fragment. Granted it's pretty platform specific.
typedef struct hwata_info_tag
{
char device[20];
char model[40];
char fwrev[8];
char serial[20];
} hwata_info_t;
/* opens the first ata harddisk on the system
and returns information about it. */
int hwata_get_info(hwata_info_t *atainfo)
{
int fd;
int result;
struct ata_params params;
const char device[] = "/dev/ad0";

fd = open(device, O_RDONLY);
if (fd < 0)
{
ERR("open device failed", errno, 1)
return(errno);
}
result = ioctl(fd, IOCATAGPARM, &params);
if (result < 0)
{
ERR("failed to get device information", errno, 1)
return(errno);
}
close(fd);
strlcpy(atainfo->device, device, sizeof(atainfo->device));
strlcpy(atainfo->model, params->model, sizeof(atainfo->model));
strlcpy(atainfo->serial, params->serial, sizeof(atainfo->serial));
strlcpy(atainfo->fwrev, params->revision, sizeof(atainfo->fwrev));
return(0);
}
And here is the compiler result:

strata:/home/dr2867/c/modules 877 $$$ ->./compile hwata hwata.c error.o
syslog.o param.o

Output File: hwata

Input Files:
hwata.c
error.o
syslog.o
param.o

gcc -W -Wall -Wshadow -Wpointer-arith -Wcast-align -Wstrict-prototypes
-Wnested-externs -Wwrite-strings -Wflo
at-equal -Winline -Wtrigraphs -ansi -std=c89 -pedantic -ggdb3 -o hwata
hwata.c error.o syslog.o param.o
hwata.c: In function `hwata_get_info':
hwata.c:82: error: invalid type argument of `->'
hwata.c:83: error: invalid type argument of `->'
hwata.c:84: error: invalid type argument of `->'

strata:/home/dr2867/c/modules 878 $$$ ->

So if this doesn't work, is there another way of doing this?
--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
Jan 26 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Daniel Rudy wrote:
>
I've ran into a little snag with the sizeof operator. Can you get the
size of a member inside a struct using sizeof?
Yes, sizeof works on just about anything.
[...]
struct ata_params params;
[...]
strlcpy(atainfo->model, params->model, sizeof(atainfo->model));
strlcpy(atainfo->serial, params->serial, sizeof(atainfo->serial));
strlcpy(atainfo->fwrev, params->revision, sizeof(atainfo->fwrev));
[...]
hwata.c:82: error: invalid type argument of `->'
hwata.c:83: error: invalid type argument of `->'
hwata.c:84: error: invalid type argument of `->'
The compiler isn't complaining about the use of sizeof. It's
complaining about using the "->" operator with the params variable,
since params is a struct (not a pointer to a struct). Replace
"params->" with "params." and the compiler should be happy.
Jan 26 '07 #2

P: n/a
Daniel Rudy wrote:
Hello Everyone,

I've ran into a little snag with the sizeof operator. Can you get the
size of a member inside a struct using sizeof? I looked through the FAQ
and I couldn't find the answer to this one.
Yes, mostly you can.
Below is the code fragment. Granted it's pretty platform specific.
<snip code>
And here is the compiler result:
<snip>
gcc -W -Wall -Wshadow -Wpointer-arith -Wcast-align -Wstrict-prototypes
-Wnested-externs -Wwrite-strings -Wflo
at-equal -Winline -Wtrigraphs -ansi -std=c89 -pedantic -ggdb3 -o hwata
hwata.c error.o syslog.o param.o
hwata.c: In function `hwata_get_info':
hwata.c:82: error: invalid type argument of `->'
hwata.c:83: error: invalid type argument of `->'
hwata.c:84: error: invalid type argument of `->'
The identifier params seems to be a struct object, not a pointer to
one, so you should use the dot operator to access the members like:

x = params.member;
So if this doesn't work, is there another way of doing this?
The sizeof statements should work. The errors are over another mistake,
as pointed above.

Jan 26 '07 #3

P: n/a

"santosh" <sa*********@gmail.comwrote in message
news:11*********************@v45g2000cwv.googlegro ups.com...
Daniel Rudy wrote:
>Hello Everyone,

I've ran into a little snag with the sizeof operator. Can you get the
size of a member inside a struct using sizeof? I looked through the FAQ
and I couldn't find the answer to this one.

Yes, mostly you can.
*mostly*

when I read the first sentence I thought it was gonna be a question like why
the following code doesnt work:

struct X
{
char buf[sizeof(struct X)];
};

Thats one of the cases where sizeof cant work (of course)
Jan 26 '07 #4

P: n/a
Serve Laurijssen wrote:
"santosh" <sa*********@gmail.comwrote in message
>Daniel Rudy wrote:
>>I've ran into a little snag with the sizeof operator. Can you
get the size of a member inside a struct using sizeof? I looked
through the FAQ and I couldn't find the answer to this one.

Yes, mostly you can.

*mostly*

when I read the first sentence I thought it was gonna be a
question like why the following code doesnt work:

struct X
{
char buf[sizeof(struct X)];
};

Thats one of the cases where sizeof cant work (of course)
sizeof is working fine there. However struct X is undefined.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews

Jan 26 '07 #5

P: n/a
CBFalconer <cb********@yahoo.comwrites:
Serve Laurijssen wrote:
>struct X
{
char buf[sizeof(struct X)];
};

Thats one of the cases where sizeof cant work (of course)

sizeof is working fine there. However struct X is undefined.
No, it's not undefined--it's incomplete. See the words of the
standard (C99 6.7.2.1):

A structure or union shall not contain a member with
incomplete or function type (hence, a structure shall not
contain an instance of itself, but may contain a pointer to
an instance of itself)...

...The [structure] type is incomplete until after the }
that terminates the [member] list.
--
"Some programming practices beg for errors;
this one is like calling an 800 number
and having errors delivered to your door."
--Steve McConnell
Jan 26 '07 #6

P: n/a
At about the time of 1/26/2007 9:40 AM, Steve Kirkendall stated the
following:
Daniel Rudy wrote:
>I've ran into a little snag with the sizeof operator. Can you get the
size of a member inside a struct using sizeof?

Yes, sizeof works on just about anything.
>[...]
struct ata_params params;
[...]
strlcpy(atainfo->model, params->model, sizeof(atainfo->model));
strlcpy(atainfo->serial, params->serial, sizeof(atainfo->serial));
strlcpy(atainfo->fwrev, params->revision, sizeof(atainfo->fwrev));
[...]
hwata.c:82: error: invalid type argument of `->'
hwata.c:83: error: invalid type argument of `->'
hwata.c:84: error: invalid type argument of `->'

The compiler isn't complaining about the use of sizeof. It's
complaining about using the "->" operator with the params variable,
since params is a struct (not a pointer to a struct). Replace
"params->" with "params." and the compiler should be happy.
That did correct the problem. Got a little too used to using pointers
to structures there. Thanks for pointing it out.

--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
Jan 27 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.