public inbox for ecos-discuss@sourceware.org
 help / color / mirror / Atom feed
From: Andrew Lunn <andrew@lunn.ch>
To: Philip Soeberg <ecos@soeberg.net>
Cc: ecos-discuss@sources.redhat.com
Subject: Re: [ECOS] Deciphering ISO C (Chap 6.3.2.3 - Pointers)
Date: Mon, 10 Nov 2003 22:16:00 -0000	[thread overview]
Message-ID: <20031110221615.GL7282@lunn.ch> (raw)
In-Reply-To: <E1AJK5Y-0004tk-00@londo.lunn.ch>

> 	cyg_uint16* res_16;
> 	cyg_uint16* p_16;
> 	cyg_uint16 u_16;
> 
> 	p_16 = 0x0u;
> 	u_16 = 0x0u;'
> 	res_16 = (p_16 + 0x555u);
> 
> The above code yields res_16 == 0xaaa
> I was sort of hoping for 0x555 instead...


> 
> Altering the addition line to this:
> 	res_16 = (cyg_uint16*)(u_16 + 0x555);
> corrects the problem... 
> 
> why?

Pointer arithmetic. Take the following example bits of code....(which probably has many syntax errors etc)

char foo[4]="bar";
char foo1[4];

int  a[]={0,1,2,3};
int  a1[3];

for (i = 0; i < 3; i++) 
{
  char * pfoo = &foo;
  char * pa = & foo1;
  
  *(pfoo+i) = pfoo[i];
  *(pa+i) = a[i];
}

Its a contrived way of copying foo into foo1 and a into a1.

You want *(pfoo+i) to be byte by byte since you are copying characters, but 
you want *(pa+i) to be word by word since its copying ints.

In 
 	res_16 = (p_16 + 0x555u);

p_16 is of type cyg_uint16 *, so you are adding 0x555 16bit words, ie 0xaaa.

With 
        res_16 = (cyg_uint16*)(u_16 + 0x555);

u_16 is a plain cyg_uint16, so its not pointer arithmetic, its normal 
arithmetic which you then cast to a pointer afterwards.

     Andrew

-- 
Before posting, please read the FAQ: http://sources.redhat.com/fom/ecos
and search the list archive: http://sources.redhat.com/ml/ecos-discuss

  parent reply	other threads:[~2003-11-10 22:16 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-11-10 20:28 [ECOS] Building from OS X Rib Rdb
2003-11-10 20:34 ` Gary Thomas
2003-11-10 22:00   ` [ECOS] Deciphering ISO C (Chap 6.3.2.3 - Pointers) Philip Soeberg
     [not found]   ` <E1AJK5Y-0004tk-00@londo.lunn.ch>
2003-11-10 22:16     ` Andrew Lunn [this message]
2003-11-11  0:27   ` [ECOS] Building from OS X Rib Rdb
2003-11-11  0:37     ` Gary Thomas
2003-11-11  0:49       ` Rib Rdb
2003-11-11  1:18         ` Rib Rdb
2003-11-13  9:37 ` [ECOS] " John Dallaway

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20031110221615.GL7282@lunn.ch \
    --to=andrew@lunn.ch \
    --cc=ecos-discuss@sources.redhat.com \
    --cc=ecos@soeberg.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).