Access array element from a pointer

mikroC, mikroBasic and mikroPascal PRO for Microchip’s 8-bit PIC MCUs.
Post Reply
Author
Message
Soumitrab
Posts: 156
Joined: 08 Jan 2012 07:28

Access array element from a pointer

#1 Post by Soumitrab » 21 Sep 2022 06:36

Hi
This is a code syntax issue that one of my team members has not been able to resolve and I'm stuck too.

We pass a pointer to an array as one of the parameters of a method and wish to access the individual elements of the said array within the function. This parameter will point to arrays of different lengths.

This is an example of the method :=

Code: Select all

const ImageArr: array [15] of byte = (0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xC0, 0x80, 0x80, 0xC0, 0xE0);

USAGE : DoSomethingHere(@ImageArr);


procedure DoSomethingHere(const AnArray: ^byte);
var
  p: ^byte;
begin
  //value of ImageArr[10] is  0xE0, how to access value of nth element of ImageArr, 

  p^ := AnArray^;
  p := p + 10; //this does not point to ImageArr[10]
end;
Thanks for your inputs

Soumitrab
Posts: 156
Joined: 08 Jan 2012 07:28

Re: Access array element from a pointer

#2 Post by Soumitrab » 21 Sep 2022 06:51

Soumitrab wrote:
21 Sep 2022 06:36

Oops, should have tried this again before posting. Anyways the correct method is as below.

Code: Select all


procedure DoSomethingHere(const AnArray: ^byte);
var
  p: ^const byte;
begin
  //value of ImageArr[10] is  0xE0, how to access value of nth element of ImageArr, 

  p := AnArray;
  p := p + 10; //this returns  ImageArr[10]
end;

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: Access array element from a pointer

#3 Post by filip » 21 Sep 2022 13:07

Hi,

The code that you posted works OK for me.

Regards,
Filip.

Post Reply

Return to “PIC PRO Compilers”