Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C Moderated > Re: Pointer ini...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 6 of 8 Topic 1096 of 1134
Post > Topic >>

Re: Pointer initialization and usage.

by Jack Klein <jackklein@[EMAIL PROTECTED] > May 9, 2008 at 05:33 PM

On Thu, 8 May 2008 02:20:26 -0500 (CDT), Vickynathan
<ragunath73@[EMAIL PROTECTED]
> wrote in comp.lang.c.moderated:

> The following code snippet works. I wonder how ? ( this is a code for
> microcontroller )
> 
> void main (void){

Before anyone else mentions it, since this is a free-standing
implementation, void main() is not necessarily undefined behavior, and
in fact is this particular implementation's normal entry point.  In
small embedded systems, there is nothing for main() to return to.

> unsigned char xdata * data pwrite;
> unsigned char code *  data pread;

You realize, I hope, that the non-standard extra keywords "xdata",
"data",  and "code" are specific to your compiler.  As it happens,
they have no real impact on your question.

> unsigned char test_array [16];
> unsigned char temp;
> ....
> ....
> ....
> 
> pwrite = (unsigned char xdata *) Scratch_Flash_Addr ;    // I have
> never seen such an initialization ??

Leaving out the non-standard "xdata" and "data", which do not matter,
what you have is:

unsigned char *pwrite;
pwrite = (unsigned char *)Scratch_Flash_Addr;

Now you haven't shown us the definition of "Scratch_Flash_Addr", but I
am going to assume that it is an integer type.

C allows you to assign or initialize a pointer type with an integer
type with a suitable cast, and here you are casting
"Scratch_Flash_Addr" to the type of pwrite.

The result of such a conversion is not guaranteed to be properly
aligned or to point to anything you can access, but it does allow
forming pointers to arbitrary areas of memory if you know what you are
doing.

> pread = my_array ;
> 
> .....
> .....
> 
> for ( i=0; i< sizeof( my_array); i++ ){
> .....
> temp = pread[i];                         // How is this possible

If you do not understand that pointers can be used with subscripts,
you really need to study some basic C.  You should also look at the
FAQ for comp.lang.c and comp.lang.c.moderated, the link is in my
signature block.  For starters, read all of section 6 about pointers
and arrays.

> without an indirection operator ?
> .....
> pwrite[i] = temp;                        // Again no indirection
> operator ??
> .....
> }

In C, the expression symbol1 [symbol2] is always an indirection
operation.  The language requires that this be translated as follows:

First, if symbol1 is an array name, it is converted to a pointer to
its first element.

After this conversion, or if symbol1 was a pointer to start with, the
expression is converted to *(symbol1 + symbol2).

-- 
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.para****ft.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
-- 
comp.lang.c.moderated - moderation address: clcm@[EMAIL PROTECTED]
 -- you must
have an appropriate newsgroups line in your header for your mail to be
seen,
or the newsgroup name in square brackets in the subject line.  Sorry.
 




 8 Posts in Topic:
Pointer initialization and usage.
Vickynathan <ragunath7  2008-05-08 02:20:26 
Re: Pointer initialization and usage.
Francis Glassborow <fr  2008-05-09 17:33:03 
Re: Pointer initialization and usage.
Kenneth Brody <kenbrod  2008-05-09 17:33:05 
Re: Pointer initialization and usage.
Vickynathan <ragunath7  2008-05-13 01:06:11 
Re: Pointer initialization and usage.
=?ISO-8859-1?Q?Hans-Bernh  2008-05-09 17:33:09 
Re: Pointer initialization and usage.
Jack Klein <jackklein@  2008-05-09 17:33:14 
Re: Pointer initialization and usage.
Vickynathan <ragunath7  2008-05-13 01:06:17 
Re: Pointer initialization and usage.
Francis Glassborow <fr  2008-05-13 01:06:20 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 15:54:55 CDT 2008.