<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.arm.com/utility/feedstylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>C51: Pointer to array type</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/38373/c51-pointer-to-array-type</link><description> 
I have defined a type for objects we use. The details of the type
should not be relevant for the users of the type, so I am &amp;quot;hiding&amp;quot; it
behind a typedef. As it happens, this type is currently implemented
as an array. 
This is problematic when we want</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: C51: Pointer to array type</title><link>https://community.arm.com/thread/125463?ContentTypeID=1</link><pubDate>Thu, 14 May 2009 07:05:53 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:c1e4eb17-acef-417d-94a5-c8e72107c788</guid><dc:creator>Asbj&amp;#195;&amp;#184;rn S&amp;#195;&amp;#166;b&amp;#195;&amp;#184;</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;&lt;br /&gt;
I&amp;#39;m convinced the compiler is quite incorrect there.&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
&lt;i&gt;Although the language definition doesn&amp;#39;t restrict what a
compiler may emit diagnostic messages about, this one is pointless
and factually wrong.&lt;br /&gt;&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
Actually, so am I now. I have read up on the spec (well not
exactly, the K&amp;amp;R2 appendix). And while an array often is
converted into a pointer to its base type, pointing to the first
element of the array, this conversion does explicitely not take place
when the array is the operand of the &amp;amp; operator. Which means that
&amp;quot;&amp;amp;array&amp;quot; should give the correct result.&lt;/p&gt;

&lt;p&gt;
I am discussing the issue with Keil.&lt;/p&gt;

&lt;p&gt;
Asbj.S.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: C51: Pointer to array type</title><link>https://community.arm.com/thread/125446?ContentTypeID=1</link><pubDate>Wed, 13 May 2009 14:17:07 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:74d91e7b-06e1-44a5-8602-5fabea396d32</guid><dc:creator>HansBernhard Broeker</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;Try type casting.&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
I would advise against that. Type casts of pointers are never the
right solution, no matter what the problem seems to be.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: C51: Pointer to array type</title><link>https://community.arm.com/thread/114913?ContentTypeID=1</link><pubDate>Wed, 13 May 2009 14:15:01 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:ecbb1d0d-4fd9-4f58-bf97-121cac87c77f</guid><dc:creator>HansBernhard Broeker</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;But how do I get the pointer to the array?&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
The same way you would do it for any other variable: by using the
&amp;#39;&amp;amp;&amp;#39; operator.&lt;/p&gt;

&lt;p&gt;
&lt;i&gt;C51 objects when I try to take the address of the array and
assign it to a pointer to the type of the array&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
I&amp;#39;m convinced the compiler is quite incorrect there.&lt;/p&gt;

&lt;p&gt;
Although the language definition doesn&amp;#39;t restrict what a compiler
may emit diagnostic messages about, this one is pointless and
factually wrong.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: C51: Pointer to array type</title><link>https://community.arm.com/thread/114925?ContentTypeID=1</link><pubDate>Wed, 13 May 2009 01:20:39 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:eb1cd440-84cd-40d2-b205-0f224eaab674</guid><dc:creator>David Brooks</dc:creator><description>&lt;p&gt;&lt;p&gt;
Try type casting. Let the compiler know exactly what you want to
do...&lt;/p&gt;

&lt;p&gt;
eg: p_object = (object_t*)(&amp;amp;my_object);&lt;/p&gt;

&lt;p&gt;
-Dave&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: C51: Pointer to array type</title><link>https://community.arm.com/thread/101832?ContentTypeID=1</link><pubDate>Tue, 12 May 2009 23:29:49 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:4faaf859-8b97-4f86-9059-9d4cd9939e1f</guid><dc:creator>Asbj&amp;#195;&amp;#184;rn S&amp;#195;&amp;#166;b&amp;#195;&amp;#184;</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;&lt;br /&gt;
Not only does the programming language have such a thing as &amp;quot;pointer
to array&amp;quot;; it even works exactly the same as it would for every other
&amp;quot;pointer to something&amp;quot;.&lt;br /&gt;&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
That is my understanding too. But how do I get the pointer to the
array?&lt;br /&gt;
As seen from my original example (relevant part of it included below)
C51 objects when I try to take the address of the array and assign it
to a pointer to the type of the array, giving a warning about
&amp;quot;pointers to different objects&amp;quot;.&lt;/p&gt;

&lt;p&gt;
With kind regards&lt;br /&gt;
Asbj.S.&lt;/p&gt;

&lt;pre&gt;
typedef char   object_t[OBJECT_SIZE];  // A type that happens to be an array

int main( void )
{
  object_t     my_object;    // An object of the given type
  object_t   * p_object;     // And a pointer to an object of the same type


  p_object = &amp;amp;my_object;     // Compiler warning - pointer to different objects

}
&lt;/pre&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: C51: Pointer to array type</title><link>https://community.arm.com/thread/89739?ContentTypeID=1</link><pubDate>Tue, 12 May 2009 13:08:59 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:66e7d73b-14c4-437d-9915-16fe79fb871d</guid><dc:creator>HansBernhard Broeker</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;The &amp;#39;C&amp;#39; programming language has no such thing;&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
That&amp;#39;s quite completely wrong.&lt;/p&gt;

&lt;p&gt;
Not only does the programming language have such a thing as
&amp;quot;pointer to array&amp;quot;; it even works exactly the same as it would for
every other &amp;quot;pointer to something&amp;quot;.&lt;/p&gt;

&lt;p&gt;
The thing that will &lt;b&gt;not&lt;/b&gt; work for an array is assignment via
that pointer, i.e. no&lt;/p&gt;

&lt;pre&gt;
*ptrToThing = *ptrToOtherThing
&lt;/pre&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: C51: Pointer to array type</title><link>https://community.arm.com/thread/101831?ContentTypeID=1</link><pubDate>Tue, 12 May 2009 06:17:57 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:5389a7e2-c1e7-4c9c-81dc-7a0b807d1755</guid><dc:creator>Asbj&amp;#195;&amp;#184;rn S&amp;#195;&amp;#166;b&amp;#195;&amp;#184;</dc:creator><description>&lt;p&gt;&lt;p&gt;
Using a struct is probably what we may end up doing. Do you have
any idea whether hiding the array in a struct will have any
performance costs, or whether the compiler will be able to see that
the address of the array is the same as the address to the struct,
and optimze it away?&lt;/p&gt;

&lt;p&gt;
Asbj.S.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: C51: Pointer to array type</title><link>https://community.arm.com/thread/77438?ContentTypeID=1</link><pubDate>Tue, 12 May 2009 06:15:33 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:499cc09d-c154-4ba7-9e74-497023b656d4</guid><dc:creator>Asbj&amp;#195;&amp;#184;rn S&amp;#195;&amp;#166;b&amp;#195;&amp;#184;</dc:creator><description>&lt;p&gt;&lt;p&gt;
Well, at least Lint seems to think there is such a thing, as given
the line (in my example)&lt;/p&gt;

&lt;pre&gt;
p_object = my_object
&lt;/pre&gt;

&lt;p&gt;
it gives the warning&lt;/p&gt;

&lt;pre&gt;
Error 64: Type mismatch (assignment) (char (*)[5] = char *)
&lt;/pre&gt;

&lt;p&gt;
which is perfectly reasonable. The pointer has been declared as a
pointer to a type which is an array[5] of chars, while &amp;quot;my_object&amp;quot;
decays into a pointer to the base type of the element, i.e. to a
pointer to char.&lt;/p&gt;

&lt;p&gt;
According to presumably knowledgeable people, the variant&lt;/p&gt;

&lt;pre&gt;
p_object = &amp;amp;my_object;
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
should be correct C and do what I want. But C51 does not seem to
agree.&lt;/p&gt;

&lt;p&gt;
Regarding the 8051: That is the processor in the chip I am using.
But we still try to maintain modularity in the code and clean
interfaces between the modules.&lt;/p&gt;

&lt;p&gt;
Thanks for your input, though!&lt;/p&gt;

&lt;p&gt;
Asbj.S.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: C51: Pointer to array type</title><link>https://community.arm.com/thread/56050?ContentTypeID=1</link><pubDate>Tue, 12 May 2009 02:26:31 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:83b77b05-39ad-4ffb-92f4-296aa3466431</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;p&gt;
I&amp;#39;m not sure that I understand what you&amp;#39;re really trying to
achieve here, but you might try Googling terms like &amp;quot;opaque type&amp;quot; and
&amp;quot;opaque pointer&amp;quot;...?&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: C51: Pointer to array type</title><link>https://community.arm.com/thread/89736?ContentTypeID=1</link><pubDate>Tue, 12 May 2009 02:01:44 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:c3702434-8c3c-4f8d-a0da-442690202d2e</guid><dc:creator>ImPer Westermark</dc:creator><description>&lt;p&gt;&lt;p&gt;
The best you can get is something like:&lt;/p&gt;

&lt;pre&gt;
typedef struct {
    unsigned char my_hidden_data[OBJECT_SIZE];
} magic_type_t;
&lt;/pre&gt;

&lt;p&gt;
The little 8051 isn&amp;#39;t the best chip in the world for data
abstraction, so doing it the computer science way may be the
difference between a working and a failed project.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: C51: Pointer to array type</title><link>https://community.arm.com/thread/56052?ContentTypeID=1</link><pubDate>Tue, 12 May 2009 01:57:18 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:6bb5105c-dd08-4597-8676-9fb70a971d15</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;&amp;quot;I need a pointer to array[OBJECT_SIZE] of char, not a plain
pointer to char.&amp;quot;&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
The &amp;#39;C&amp;#39; programming language has no such thing; nothing
specifically to do with C51 - that just the way the language
works.&lt;/p&gt;

&lt;p&gt;
Are you sure that the 8051 is a good choice for this kind of
programming?&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>