sizeof operator question

D

Daniel Rudy

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
 
S

Steve Kirkendall

Daniel said:
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.
 
S

santosh

Daniel said:
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.
And here is the compiler result:
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.
 
S

Serve Laurijssen

santosh said:
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)
 
C

CBFalconer

Serve said:
santosh said:
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
 
B

Ben Pfaff

CBFalconer said:
Serve said:
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.
 
D

Daniel Rudy

At about the time of 1/26/2007 9:40 AM, Steve Kirkendall stated the
following:
Daniel said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top