473,396 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

sizeof operator question


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
6 2620
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
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

"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

70
by: Roy Yao | last post by:
Does it mean "(sizeof(int))* (p)" or "sizeof( (int)(*p) )" ? According to my analysis, operator sizeof, (type) and * have the same precedence, and they combine from right to left. Then this...
7
by: dam_fool_2003 | last post by:
#include<stdio.h> int main(void) { unsigned int a=20,b=50, c = sizeof b+a; printf("%d\n",c); return 0; } out put: 24
42
by: Christopher C. Stacy | last post by:
Some people say sizeof(type) and other say sizeof(variable). Why?
55
by: Robotnik | last post by:
Hello All, I want to know if we could know the size of a structyure without the use of sizeof(). Any hints.
8
by: Mallesh | last post by:
Can anybody tell me how sizeof operator internally implemented. In my project i want to implemnent my own sizeof operator function (like mysizeof). How i can write the code? Please help me.
36
by: Frederick Gotham | last post by:
sizeof has the same precedence as a cast, and they both bind from right to left. The following won't compile for me with gcc: int main(void) { sizeof(double)5; return 0; }
3
by: venkat | last post by:
Hi, I am new to c programming language. I heard that , sizeof can be made as run time operator. Even though it is a compile time operator. I am not sure also whether sizeof can be made as run time...
72
by: goacross | last post by:
char ch='a'; int v=sizeof ++ch; cout<<ch<<endl;// output: 'a' why not 'b'? thanks
11
by: Richard Tobin | last post by:
Please excuse me if this has already been covered. Given char x; is sizeof(x)
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.