Yeah, I was going to point that out. The problem is that the %d conversion
consumes an int, while sizeof produces a value of type size_t, which is often a
long int. On the Palm, the two are not the same size, and so StrPrintF gets out
of sync.
I'd write that line as:
StrPrintF(msg, "sz(p) is %d; sz(*p) is %d; sz(a) is %d",
(int) sizeof(p), (int) sizeof(*p), (int) sizeof(a));
or:
StrPrintF(msg, "sz(p) is %ld; sz(*p) is %ld; sz(a) is %ld",
(long) sizeof(p), (long) sizeof(*p), (long) sizeof(a));
(...and hope that someone doesn't point out that I should be using the
*unsigned* conversion operators...)
-- Keith Rollin
-- Palm OS Emulator engineer
"Richard Burmeister" <[EMAIL PROTECTED]> on 12/11/2000 12:17:12 PM
Please respond to "Palm Developer Forum" <[EMAIL PROTECTED]>
Sent by: "Richard Burmeister" <[EMAIL PROTECTED]>
To: "Palm Developer Forum" <[EMAIL PROTECTED]>
cc: (Keith Rollin/US/PALM)
Subject: Re: an easy C question...
Dave,
Your code example is not as useful in making your point as you might think.
What your C compiler does on a Unix or Linux box is not necessarily what
will happen on a Palm OS device.
For example, using CodeWarrior and running on palmos35-dr4-en-color.rom, the
equivalent code gives different results.
Char *p;
Char a[20];
Char msg[50];
StrPrintF(msg, "sz(p) is %d; sz(*p) is %d; sz(a) is %d", sizeof(p),
sizeof(*p), sizeof(a));
WinDrawChars(msg, StrLen(msg), 5, 20);
And the output is ...
sz(p) is 0; sz(*p) is 4; sz(a) is 0
----- Original Message -----
From: "Dave Carrigan" <[EMAIL PROTECTED]>
To: "Palm Developer Forum" <[EMAIL PROTECTED]>
Sent: Monday, December 11, 2000 1:43 PM
Subject: Re: an easy C question...
> "Steve Austin" <[EMAIL PROTECTED]> writes:
>
> > I guess I must be missing something.
> >
> > If you have:
> >
> > char s[8];
> >
> > and you call:
> >
> > sizeof(s) shouldn't you get the size of a const char pointer on your
system
> > (probably 4)? After all that's what s really is.
>
> No, that actually is not what s really is. For most practical purposes,
> char* and char[] can be used interchangeably, but they are not the
> same. The sizeof operator returns the amount of memory that an
> identifier uses, and it will be different for char* and char[].
>
> Observe:
>
> $ cat > test.c
> int main()
> {
> char *p;
> char a[20];
> printf("sizeof(p) is %d; sizeof(*p) is %d; sizeof(a) is %d\n", sizeof(p),
sizeof(*p), sizeof(a));
> }
> $ make test
> cc test.c -o test
> $ ./test
> sizeof(p) is 4; sizeof(*p) is 1; sizeof(a) is 20
>
> --
> Dave Carrigan ([EMAIL PROTECTED]) | Yow! ... Blame it on the
BOSSA
> UNIX-Apache-Perl-Linux-Firewalls-LDAP-C-DNS | NOVA!!!
> Seattle, WA, USA |
> http://www.rudedog.org/ |
>
> --
> For information on using the Palm Developer Forums, or to unsubscribe,
please see http://www.palmos.com/dev/tech/support/forums/
>
--
For information on using the Palm Developer Forums, or to unsubscribe, please
see http://www.palmos.com/dev/tech/support/forums/
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palmos.com/dev/tech/support/forums/