<?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>what is the matter with the pointer?</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/17712/what-is-the-matter-with-the-pointer</link><description> hello all! 
 
*.h 
void Baudrate(unsigned int *ptr,unsigned char Uart_num) large; 
 
*.c 
 
void main(void) 
{ 
... 
*ptr = 1152; 
Baudrate(ptr,1); 
... 
} 
In the routine ,the var Baud( Baud = *ptr; )is not 1152 but 0x00.When changing the defintion</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: what is the matter with the pointer?</title><link>https://community.arm.com/thread/134524?ContentTypeID=1</link><pubDate>Wed, 16 Mar 2005 07:45:11 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:8d9dec77-8762-4789-b8a0-abe465821d3f</guid><dc:creator>John Donaldson</dc:creator><description>&lt;p&gt;&amp;quot;This shows that pointer is placed inside idata memory and points to xdata memory.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Sorry, I should have explained that. I didn&amp;#39;t realise it was a question. The correct syntax would be:&lt;br /&gt;
&lt;br /&gt;
char xdata * idata ptr;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: what is the matter with the pointer?</title><link>https://community.arm.com/thread/122718?ContentTypeID=1</link><pubDate>Wed, 16 Mar 2005 06:27:42 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:73422071-7501-4bf2-9cba-2db926bc104c</guid><dc:creator>Oleg Sergeev</dc:creator><description>&lt;p&gt;hi,&lt;br /&gt;
&lt;br /&gt;
I just compiled next example:&lt;br /&gt;
&lt;pre&gt;
#include &amp;lt;reg51.h&amp;gt;

void main(void)
{
idata char xdata *ptr;

	ptr = 0x11;
	*ptr = 0x22;
	while (1);
}&lt;/pre&gt;
The result assembly code is:&lt;br /&gt;
&lt;pre&gt;
; ptr = 0x11;
MOV      R0,#0x08
MOV      @R0,#0x00
INC      R0
MOV      @R0,#0x11
; *ptr = 0x22;
DEC      R0
MOV      A,@R0
MOV      R6,A
INC      R0
MOV      A,@R0
MOV      DPL,A
MOV      DPH,R6
MOV      A,#0x22
MOVX     @DPTR,A
&lt;/pre&gt;
This shows that pointer is placed inside idata memory and points to xdata memory.&lt;br /&gt;
&lt;br /&gt;
Regards,&lt;br /&gt;
Oleg&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: what is the matter with the pointer?</title><link>https://community.arm.com/thread/111340?ContentTypeID=1</link><pubDate>Wed, 16 Mar 2005 03:34:39 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:f971df32-fa4e-4d49-9b09-e31c42835cf6</guid><dc:creator>John Donaldson</dc:creator><description>&lt;p&gt;&amp;quot;unsigned int xdata *ptr;&lt;br /&gt;
xdata unsigned int *ptr;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
I believe the second example is obsolete syntax supported for backward compatibility. The new way of doing things is:&lt;br /&gt;
&lt;br /&gt;
char xdata * data ptr;&lt;br /&gt;
&lt;br /&gt;
Which is a pointer located in data space pointing to a char in xdata space. I think this makes things a bit easier as you only have to remember that the qualifier to the left of the &amp;#39;*&amp;#39; specifies the location of the object being pointed to and the qualifier to the right of the &amp;#39;*&amp;#39; specifies the location of the pointer.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Finally, to confuse you completely, answer what does next line do yourself:&lt;br /&gt;
&lt;br /&gt;
idata unsigned int xdata *ptr;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Yes, well, quite.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: what is the matter with the pointer?</title><link>https://community.arm.com/thread/96999?ContentTypeID=1</link><pubDate>Wed, 16 Mar 2005 01:10:37 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:39e9e6bd-dc2d-483b-870a-30d5aa135d36</guid><dc:creator>Oleg Sergeev</dc:creator><description>&lt;p&gt;hi,&lt;br /&gt;
here is a small example which could help you to see what is wrong. Please note that this is for &amp;quot;classic&amp;quot; C; for Keil please read notes below.&lt;br /&gt;
&lt;pre&gt;
void main(void)
{

// Next line allocates memory where pointer is placed in.
// Depend on compiler it may take different number of bytes.
// Please note that this does not hold any value,
// This just reserves a couple of bytes for pointer
unsigned int *ptr;


// Next line initializes the pointer.
     ptr = 0x1111;
// Now the pointer points to the memory location 0x1111.


// Here we load value 0x2222 in memory at address 0x1111.
     *ptr = 0x2222;
// Depend on compiler and memory model
// this loads two or four (or?) bytes in
// sequential memory locations 0x1111, 0x1112, ...
}
&lt;/pre&gt;
&lt;br /&gt;
Now about Keil.&lt;br /&gt;
Generally, it is very bad idea to use pointer to undefined data/memory types.&lt;br /&gt;
For example, when you type:&lt;br /&gt;
&lt;pre&gt;unsigned int *ptr;&lt;/pre&gt;
then compiler does not know to which memory the pointer points in. Compiler then tries internal function C?ISTPTR which cannot recognize memory type and may (by my tests) truncate pointer/value and produce total wrong code even without any warning!&lt;br /&gt;
So the correct syntax should be:&lt;br /&gt;
&lt;pre&gt;unsigned int xdata *ptr;
unsigned int idata *ptr;
&lt;/pre&gt;
etc&lt;br /&gt;
&lt;br /&gt;
With this way you notice compiler that the pointer points into xdata, idata etc memory segment. Now compiler has no problems and produces short, fast and correct code.&lt;br /&gt;
By the way, it is important that you understand syntax right. For example, next two lines produce total different code:&lt;br /&gt;
&lt;pre&gt;unsigned int xdata *ptr;
xdata unsigned int *ptr;&lt;/pre&gt;
First line asks compiler to create a pointer which points into xdata memory. Second line just tells compiler that a pointer must be placed inside xdata memory.&lt;br /&gt;
Finally, to confuse you completely, answer what does next line do yourself:&lt;br /&gt;
&lt;pre&gt;idata unsigned int xdata *ptr;&lt;/pre&gt;
&lt;br /&gt;
Regards,&lt;br /&gt;
Oleg&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: what is the matter with the pointer?</title><link>https://community.arm.com/thread/111351?ContentTypeID=1</link><pubDate>Tue, 15 Mar 2005 12:23:07 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:d98dc132-4175-4863-8ad3-ad17fdeb0a71</guid><dc:creator>Drew Davis</dc:creator><description>&lt;p&gt;As Hans says, ptr in main() is uninitialized.  It could point anywhere, including an address where there is no RAM to store a value.  Odds are, a garbage pointer happens to point to zero.&lt;br /&gt;
&lt;br /&gt;
Perhaps the ellipsis hides the initialization.&lt;br /&gt;
&lt;br /&gt;
There&amp;#39;s no real reason to pass by reference in this case.  Stylistically, I&amp;#39;d write&lt;br /&gt;
&lt;br /&gt;
U16 GetBaudRate (U8 uartNumber);&lt;br /&gt;
&lt;br /&gt;
This will also lead to smaller and faster code, particularly compared to using the 3-byte generic pointer to pass a 2-byte int.&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;large&amp;quot; qualifier on the function makes me wonder.  If this keyword changes the default pointer type for the parameter, and the caller is using a different memory model, the parameter might be confused without an explicit cast.  But I&amp;#39;m feeling too lazy to test this hypothesis right now.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: what is the matter with the pointer?</title><link>https://community.arm.com/thread/96996?ContentTypeID=1</link><pubDate>Tue, 15 Mar 2005 08:17:29 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:057b7d9e-1eeb-4ead-bd24-d2a22b06d0a7</guid><dc:creator>HansBernhard Broeker</dc:creator><description>&lt;p&gt;Please distill this down to a &lt;b&gt;complete&lt;/b&gt;, self-contained, compilable example case. Nobody can diagnose what&amp;#39;s going wrong from a loose collection of snippets ripped from their context like the one you&amp;#39;ve decided to show, so far.&lt;br /&gt;
&lt;br /&gt;
For starters, your &amp;#39;ptr&amp;#39; currently points nowhere.  That&amp;#39;s &lt;b&gt;bad&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
Furthermore the observed behaviour could easily happen if the source file containing your &amp;#39;main&amp;#39; didn&amp;#39;t actually #include the header containing the prototype for Baudrate(), or if the prototype were #ifdef&amp;#39;ed out of sight, or if the actual definition of Baudrate (in some other .c file?) didn&amp;#39;t agree with the prototype declaration.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: what is the matter with the pointer?</title><link>https://community.arm.com/thread/73103?ContentTypeID=1</link><pubDate>Tue, 15 Mar 2005 07:57:07 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:c8a8b82e-13b2-4612-894c-f58af2e1fcc1</guid><dc:creator>Nantian Gumo</dc:creator><description>&lt;p&gt;The pointer is defined in the main.c as &amp;quot;unsigned int *ptr;&amp;quot;.&lt;br /&gt;
 *.c:&lt;br /&gt;
&lt;br /&gt;
void main(void)&lt;br /&gt;
{    ...&lt;br /&gt;
     unsigned int *ptr;&lt;br /&gt;
     ...&lt;br /&gt;
     *ptr = 1152;&lt;br /&gt;
     Baudrate(ptr,1);&lt;br /&gt;
     ...&lt;br /&gt;
}&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: what is the matter with the pointer?</title><link>https://community.arm.com/thread/43701?ContentTypeID=1</link><pubDate>Tue, 15 Mar 2005 03:15:03 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:161628b6-4155-4037-9738-8279bd6ee7ad</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;Where&amp;#39;s your definition of &lt;b&gt;ptr&lt;/b&gt;?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>