char (*array_p) [1024] question 
Author Message
 char (*array_p) [1024] question

While I think I understand this, I'm having a hard time fully grasping
its import. Could someone kinda lay it out for me more clearly than my
aging brain is seeing it?

Given:

#include <stdio.h>

static char array [1024] = "Test!";

int main (void)
{
    char  (*array_p) [1024] = &array;

    printf ("array = %p\n", array);
    printf ("array = %p\n", *array);
    printf ("array = %s\n", array);

    printf ("array_p = %p\n", array_p);
    printf ("array_p = %p\n", *array_p);
    printf ("array_p = %s\n", array_p);
    printf ("array_p = %s\n", *array_p);

    return 0;

Quote:
}

I get:

array = 400010d0
array = 00000054
array = Test!
array_p = 400010d0
array_p = 400010d0
array_p = Test!
array_p = Test!

My problem is that, while I think I "get it", my mind is being twisted
by the apparent equality of array_p and *array_p. My real question, as
I'm presented with a vendor library with dozens of definitions along
the lines of array_p, is which is preferable, the dereference or just
the reference (i.e. *array_p or array_p)?

p.s.
Would appreciate email copy of any replies, as our newsfeed is unreliable
at best.

tanks,
--

Engineering Information Services/Information Technology/3M, St.Paul, Minn
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The brain is nothing without imagination.



Thu, 15 Apr 1999 02:00:00 GMT  
 char (*array_p) [1024] question

Quote:

> While I think I understand this, I'm having a hard time fully grasping
> its import. Could someone kinda lay it out for me more clearly than my
> aging brain is seeing it?

> Given:

> #include <stdio.h>

> static char array [1024] = "Test!";

> int main (void)
> {
>     char  (*array_p) [1024] = &array;

>     printf ("array = %p\n", array);

%p needs a void*, you are passing it a char*. This leads to undefined
behaviour, but because of a footnote in the standard, it will work for
`most' implementations. Pass (void*)array instead.

Quote:
>     printf ("array = %p\n", *array);

This time you are passing it an int (which came from promotion of a
char). Not good at all. A (void*) cast will make it implementation
defined.

Quote:
>     printf ("array = %s\n", array);

>     printf ("array_p = %p\n", array_p);

This again needs a (void*) cast. There is no footnote to save you now
:-)

Quote:
>     printf ("array_p = %p\n", *array_p);

Add a (void*) cast.

Quote:
>     printf ("array_p = %s\n", array_p);

This time printf is expecting a pointer to the first element of a
character array containing a string (forget bugs in the standard). You
are instead passing it a char(*)[1024] which is bad (modulo the bug in
the standard).

- Show quoted text -

Quote:
>     printf ("array_p = %s\n", *array_p);

>     return 0;
> }

> I get:

> array = 400010d0
> array = 00000054
> array = Test!
> array_p = 400010d0
> array_p = 400010d0
> array_p = Test!
> array_p = Test!

> My problem is that, while I think I "get it", my mind is being twisted
> by the apparent equality of array_p and *array_p. My real question, as

Okay, array_p points to the array. *array_p IS the array. Now, an
array in contexts where its value is called for becomes a pointer to
the first character. On your implementation, conversion to (void*)
happens not to do anything (so, you got away with it), pointers do not
store type information, does not store bounds information and only
stores the abstract `machine address' of the object. As this `address'
of an array and that of its first element are the same, array_p and
*array_p when used in this context give you the same result.

Note carefully that there were a number of things one had to assume
about your implementation to get at this behaviour.

Quote:
> I'm presented with a vendor library with dozens of definitions along
> the lines of array_p, is which is preferable, the dereference or just
> the reference (i.e. *array_p or array_p)?

They are different. array_p is a pointer to a whole array. array_p+1
will point beyond the whole array. sizeof(array_p) will be the size of
the pointer. *array_p is the whole array. sizeof(*array_p) will give
you the size of this object. In uses where a value, rather than an
object, is called for, it will be converted to a pointer to the first
element of the array. Thus *array_p+1 will point to beyond the first
element of the array, i.e. at the second element, i.e. it will be
&array[1].

Cheers
Tanmoy
--

Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
<http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]



Fri, 16 Apr 1999 03:00:00 GMT  
 char (*array_p) [1024] question

Quote:

>Given:
>#include <stdio.h>
>static char array [1024] = "Test!";
>int main (void)
>{
>    char  (*array_p) [1024] = &array;

        This is an unusual declaration.  It states that array_p is a
pointer to an array with 1024 char elements.

        Remember that arrays and pointers are not the same thing but that
an array (almost always) 'decays' into a pointer to the first element of
the array when it is used as an lvalue.  One of the exceptions to this
rule is when the array is preceeded by an '&' as in the declaration above.
In this case array_p is simply initialised with a pointer to the array,
i.e. a pointer to the first element of the array as you would expect.

        What happens if we dereference the pointer array_p (i.e. use the
expression *array_p)?  If I have a pointer to an int dereferencing it will
give me an int.  Likewise, given array_p, a pointer to an array of chars,
dereferencing it will give me an array of chars, which decays into a
pointer to the first element of the array.  In other words array_p and
*array_p will give you the same result in the code below.  This does not
mean that they are the same TYPE however, see below.

Quote:
>    printf ("array = %p\n", array);
>    printf ("array = %p\n", *array);

        This does not make sense.  If array is an array of chars, when it
is used in this expression it decays into a pointer to the first element
(char) of the array.  Dereferencing it will give a char which is promoted
to an int.  Use '%c' to display the character or '%d' or '%x' the value in
decimal or hexidecimal respectively.

Quote:
>    printf ("array = %s\n", array);
>    printf ("array_p = %p\n", array_p);
>    printf ("array_p = %p\n", *array_p);
>    printf ("array_p = %s\n", array_p);
>    printf ("array_p = %s\n", *array_p);
>    return 0;
>}
>My problem is that, while I think I "get it", my mind is being twisted
>by the apparent equality of array_p and *array_p. My real question, as
>I'm presented with a vendor library with dozens of definitions along
>the lines of array_p, is which is preferable, the dereference or just
>the reference (i.e. *array_p or array_p)?

The only use I can think of for pointers to whole arrays, would be in
handling arrays of arrays (I would be interested in hearing of any
others!).  As for which to use there is a very important distinction which
your example code did not show.  Although the value returned by array_p
and *array_p may be the same THEY ARE NOT THE SAME TYPE.  Hence,
array_p[1] and (*array_p)[1] are completely different!

Hope this helps,

Keith.
--
Keith A Goatman                        Medical Physics and Clinical Eng.
                                       Royal Hallamshire Hospital

     *** Please DO NOT add my address to any mailing lists ***



Fri, 16 Apr 1999 03:00:00 GMT  
 char (*array_p) [1024] question

Quote:

> [...very detailed explanation...]

Thanks to you and others who wrote. My brain was obviously on hold that
day, since a lot of it suddenly became clear while I was driving home
that night. Too much work on an inherited project that's broken, way
over schedule and budget.  <sigh> The life of a working programmer....

The rather blindingly obvious difference between:

    char  array [1024];
    char (*array_p) [1024];

is quite apparent when you try to pass'm to a function that proto's like:

    int  a_function (char* arg);

Now I'll go and again read the standard cover to cover to make up for
asking a stupid question.....  ;->

--

Engineering Information Services/Information Technology/3M, St.Paul, Minn
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Usenet is like a town square. Beware the dogma droppings.



Fri, 16 Apr 1999 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. converting const char* to unsigned char[1024]

2. please any one help me in dividing 1024 bit number with 1024 bit another number

3. ETDT_ENABLETAB background repeats at 1024 pixels - aggggh

4. 1024 letters in command line

5. How to listen to a port < 1024

6. how to replace /e:1024 by /e:2048 ?

7. Misleading Disk full error when writing files beyond 32767 * 1024 bytes

8. 1024 * 768 Resolution?

9. 1024*768 graphic

10. A char pointer (char *) vs. char array question

11. (THE simplest question ever) char[] when the required argument is a char *

12. a char and unsigned char question again

 

 
Powered by phpBB® Forum Software