Re: pointing to a memory position

Jul 04, 2003 0 Replies


>


> > > If logical memory = physical memory and you have no memory protection
> > > support from the OS then just:
> >
> > > char *pFoo = (char *) 0x0A20;
> >
> > Careful there. He was talking about other code modifying that memory
> > content behind the back of the C(++) code. He'll thus need a
> > 'volatile' qualification. Otherwise the compiler's optimization will
> > break his code sonner or later.
> >
> > volatile char *pFoo = (char *)0x0a20;
>
> How do you do an indexed read of memory location 0x0A20 using a
> variable as an index?
>
> i.e.
> int Index , Read;
> Index = 5;
> Read = pFoo[Index]; /* contents of (0x0A20 + 5) -> read */
>

That's OK, just go ahead.



In C, an array name is a constant pointer to the array base address, and an indexing operation is equivalent to using a pointer constructed as the sum of the base pointer and index value.



If there is:



char arr[20],



arr is a constant char* pointing to the base of the array.



arr[i] is equivalent to *(arr + i)



A word of caution:



The indexing in C takes the size of the array base type into account, so the byte index into an int array may produce a surprise. In indexing, the machine byte address is increased by the amount of sizeof(base type)*index.



So, to access a Flash chip residing at 0x01000000:



unsigned char *ptr = (unsigned char *)0x01000000;



ptr[256] is the byte at address 0x01000100.



HTH



Tauno Voipio tauno voipio @ iki fi


Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required