Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Address of array && address of pointer to array |
| Date | 2011-09-16 02:09 -0700 |
| Organization | None to speak of |
| Message-ID | <lnbouk97lk.fsf@nuthaus.mib.org> (permalink) |
| References | (1 earlier) <0.043e10b933bc7ff0fade.20110914144111BST.87sjnzz1fs.fsf@bsb.me.uk> <a5df2719-174b-41e0-a39d-659878de44f3@h7g2000yqm.googlegroups.com> <4E720A7B.2010401@verizon.net> <4E72516D.5E80@mindspring.com> <fb1e1749-853e-4399-ac85-dacae2b3110f@h6g2000yqe.googlegroups.com> |
Stanley Rice <heconghui@gmail.com> writes:
> On Sep 16, 3:26 am, pete <pfil...@mindspring.com> wrote:
>> James Kuyper wrote:
>>
>> > On 09/15/2011 08:22 AM, Stanley Rice wrote:
>> > > So What you mean is that the compiler does most of things for us? I
>> > > have tried to compare the address of function, say
>>
>> > > void foo() { }
>>
>> > > assert((void *)foo == (void *)&foo);
>>
>> > > and it passes. Is it the reason as the issue of the array talk above ?
>>
>> > No, the rule for arrays doesn't apply here. However, as he said above,
>> > there is a similar rule for functions,
>> > and that is what makes this code work.
>>
>> That code would be fine without the casts,
>>
>> assert(foo == &foo);
>>
>> but
>> the result of a pointer to a function
>> being cast to a pointer to an incomplete type such as (void *),
>> is undefined.
>>
>> --
>> pete
>
> Err, I seems that I am just a novice and I don't fully understand the
> pointer. Why should a pointer have to an object that has a specific
> type, even,(void). Every code and variable and anything else just
> store in the memory. Does the pointer with the specific type to point
> to locate how much memory space to charge?
The type of a pointer is generally a compile-time concept. For example,
given:
short *p1;
double *p2;
p1 and p2 *might* have the same size and representation, but they're of
different types. That means, among other things, that the expression
``*p1'' is of type short, the expression ``*p2'' is of type double, and
the assignment ``p1 = p2'' is invalid, since the types are incompatible.
(I'm not using the term "incompatible" quite the same way the standard
does.)
And consider a system where int and long have exactly the same size and
representation (say they're both 4 bytes). int* and long* are still two
distinct types, and the compiler won't allow you to assign one to the
other without a cast (or at least it will warn you if you attempt to do
so). There's more to type than size.
> For example, a pointer points to type int means that 4 bytes of memory
> is in the charge of the pointer, and a pointer points to type char
> means that 1 byte of memory is in the charge of the pointer.
Pretty much, though I'm not sure what "in the charge of" means
here. Also, an int isn't necessarily 4 bytes, though a char is by
definition 1 byte (but a byte can be more than 8 bits).
> In this
> way, when we dereference a pointer that points to type int, we can get
> a 4 byte integer, and when we dereference a pointer to char, we can
> get one byte character. Is that right? And I wrote a testing program,
> the snippet is as below:
>
> int a = 48; // 48 represents character '0' in ASCII
ASCII is not the only character set; this would be better written as
int a = '0';
> int *b = &a;
> char *c = (char *)b;
This is not reliable. It causes c to point to the first byte of a;
that's not necessarily the low-order byte.
> printf("%c\n", c); //2
I think you meant
printf("%c\n", *c);
> The printf statement prints the character '0'. I guess the reason is
> that,according to the little-endian priciple, integer 48 stores in the
> side of least significant bit, but its memory address is lowest. When
> I cast pointer b explicitly with (char *) to pointer c that points to
> a char. The new pointer c, points to the original address where b ever
> points to, but the difference is that, the pointer c that points to a
> char can only in charge of one byte, that is, the lowest byte. so, '0'
> is printed.
Yes, but little-endianness is not a principle, it's merely an
implementation choice.
> To prove my assumption, I try to change my code, In the first
> statement, I change integer 48 to 304 (304 = 256 + 48), the least
> sinificant byte is still hold integer 48, but the second least
> significant byte hold an integer 1. In this way, the printf statement
> still print character '0'.
>
> So I guess the use of pointer pointed type is to determine how many
> byte could this pointer in charge from the least significant byte. But
> I don't know whether I'm right or not.
It's the lowest addressed byte, not necessarily the least significant
byte.
> If my assumption is correct, then may be I can guess why casting a
> pointer to function to void* is undefined. The pointer points to the
> starting address of the function, without knowing the whole length of
> the function, so the pointer doesn't know how many bytes could the
> pointer in charge of, right?
A function pointer isn't necessarily the memory address of the start
of the function. On most systems you're likely to use, it probably
will be, but the standard doesn't guarantee it. For example, it
could be an index into a table of all the functions in your program.
Or it could contain additional system-specific information. (As I
recall, function pointers on the IBM AS/400 are something like
16 bytes, though it's very likely I've got the details wrong.)
The standard only requires that function pointers work the way
they're supposed to: you can call functions through them, distinct
functions have distinct addresses, and so on.
Conversion from a function pointer to void* is undefined simply
because the standard doesn't define it -- and the reason for that
is that any definition could make a correct implementation difficult
or impossible on some systems.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Address of array && address of pointer to array Stanley Rice <heconghui@gmail.com> - 2011-09-14 06:12 -0700
Re: Address of array && address of pointer to array Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-14 14:41 +0100
Re: Address of array && address of pointer to array Paul N <gw7rib@aol.com> - 2011-09-14 14:15 -0700
Re: Address of array && address of pointer to array Peter Nilsson <airia@acay.com.au> - 2011-09-14 19:25 -0700
Re: Address of array && address of pointer to array Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-15 10:54 +0100
Re: Address of array && address of pointer to array Stanley Rice <heconghui@gmail.com> - 2011-09-15 05:36 -0700
Re: Address of array && address of pointer to array Ike Naar <ike@sverige.freeshell.org> - 2011-09-15 13:46 +0000
Re: Address of array && address of pointer to array Nobody <nobody@nowhere.com> - 2011-09-15 14:48 +0100
Re: Address of array && address of pointer to array Keith Thompson <kst-u@mib.org> - 2011-09-15 10:28 -0700
Re: Address of array && address of pointer to array Stanley Rice <heconghui@gmail.com> - 2011-09-15 18:23 -0700
Re: Address of array && address of pointer to array Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-16 12:58 +0100
Re: Address of array && address of pointer to array James Kuyper <jameskuyper@verizon.net> - 2011-09-16 08:03 -0400
Re: Address of array && address of pointer to array Nobody <nobody@nowhere.com> - 2011-09-16 13:09 +0100
Re: Address of array && address of pointer to array Keith Thompson <kst-u@mib.org> - 2011-09-16 12:05 -0700
Re: Address of array && address of pointer to array gazelle@shell.xmission.com (Kenny McCormack) - 2011-09-16 19:35 +0000
Re: Address of array && address of pointer to array Peter Nilsson <airia@acay.com.au> - 2011-09-19 15:45 -0700
Re: Address of array && address of pointer to array Keith Thompson <kst-u@mib.org> - 2011-09-19 17:47 -0700
Re: Address of array && address of pointer to array James Kuyper <jameskuyper@verizon.net> - 2011-09-15 10:21 -0400
Re: Address of array && address of pointer to array Stanley Rice <heconghui@gmail.com> - 2011-09-15 05:22 -0700
Re: Address of array && address of pointer to array James Kuyper <jameskuyper@verizon.net> - 2011-09-15 10:23 -0400
Re: Address of array && address of pointer to array pete <pfiland@mindspring.com> - 2011-09-15 15:26 -0400
Re: Address of array && address of pointer to array Stanley Rice <heconghui@gmail.com> - 2011-09-15 18:14 -0700
Re: Address of array && address of pointer to array Keith Thompson <kst-u@mib.org> - 2011-09-16 02:09 -0700
Re: Address of array && address of pointer to array James Kuyper <jameskuyper@verizon.net> - 2011-09-16 08:39 -0400
Re: Address of array && address of pointer to array pete <pfiland@mindspring.com> - 2011-09-16 18:08 -0400
Re: Address of array && address of pointer to array Bart van Ingen Schenau <bart@ingen.ddns.info> - 2011-09-16 16:38 +0200
Re: Address of array && address of pointer to array cri@tiac.net (Richard Harter) - 2011-09-16 22:04 +0000
Re: Address of array && address of pointer to array Stanley Rice <heconghui@gmail.com> - 2011-09-15 18:08 -0700
Re: Address of array && address of pointer to array Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-16 13:12 +0100
Re: Address of array && address of pointer to array James Kuyper <jameskuyper@verizon.net> - 2011-09-16 08:30 -0400
Re: Address of array && address of pointer to array Keith Thompson <kst-u@mib.org> - 2011-09-15 11:51 -0700
Re: Address of array && address of pointer to array Stanley Rice <heconghui@gmail.com> - 2011-09-15 18:13 -0700
Re: Address of array && address of pointer to array James Kuyper <jameskuyper@verizon.net> - 2011-09-15 21:18 -0400
Re: Address of array && address of pointer to array Stanley Rice <heconghui@gmail.com> - 2011-09-16 05:37 -0700
Re: Address of array && address of pointer to array James Kuyper <jameskuyper@verizon.net> - 2011-09-16 09:46 -0400
Re: Address of array && address of pointer to array Keith Thompson <kst-u@mib.org> - 2011-09-16 12:08 -0700
Re: Address of array && address of pointer to array Nobody <nobody@nowhere.com> - 2011-09-16 13:20 +0100
csiph-web