Access via pointer failes

General discussion on mikroPascal PRO for AVR.
Post Reply
Author
Message
crestfallen
Posts: 15
Joined: 01 Jan 2009 14:16

Access via pointer failes

#1 Post by crestfallen » 07 Mar 2012 17:48

Hello,

I have extracted the following code from a larger project.
The problem is, that i'm not able to pass the array via an adresspointer in the procedure SPI_Write_Buf() with three parameters.
In the procedure with only one parameter it seems to work.

I think the problem is, that the correct adress from "globalpointer" isn't copied to "apointer".

Code: Select all

program Zeigerproblem01;

var WriteArr: array[4] of byte;
var globaltextbuffer: array[8] of char;
var globalcounter: byte;
var globalpointer: ^byte;

procedure test(matrix: ^byte);
begin
     for globalcounter := 0 to 3 do
     begin
       matrix^ := matrix^+1;
       Inc(matrix);
     end;
end;

procedure SPI_Write_Buf(reg: byte; apointer: ^byte; bytelength:byte);
var byte_ctr: byte;
var op_status: byte;

begin
  UART1_Write_text('adr_proc ');
  ByteToStr(apointer^,globaltextbuffer); //Returns Wrong Value
  UART1_Write_text(globaltextbuffer);


  {for globalcount := 0 to 3 do                          //WORKS
  begin
        ByteToStr(WriteArr[globalcount],globaltextbuffer);
        UART1_Write_text(globaltextbuffer);
  end;

  for globalcount := 0 to 3 do                          //Manipulating Array-> Works
  begin
        globalpointer^ := globalpointer^+1;
        Inc(globalpointer);
  end;

  for globalcount := 0 to 3 do                          //WORKS
  begin
        ByteToStr(WriteArr[globalcount],globaltextbuffer);
        UART1_Write_text(globaltextbuffer);
        Dec(globalpointer);                               //Dirty piece of code to give pointer initial value
  end;

   for globalcount := 0 to 3 do                          //Works
  begin
        ByteToStr(globalpointer^,globaltextbuffer);
        UART1_Write_text(globaltextbuffer);
        Inc(globalpointer);
  end; }

  UART1_Write_Text('value_proc ');
  //anbuffer := globalpointer;            //doesn't help
  for globalcounter := 0 to 3 do            //FAILES -RETURNS WRONG VALUES-
  begin
        ByteToStr((apointer^),globaltextbuffer);
        UART1_Write_text(globaltextbuffer);
        inc(apointer);
  end;
end;

begin
{ Main program }
    UART1_Init(19200);
    delay_ms(100);
    UART1_Write_text('Init');

    WriteArr[0] := 1;
    WriteArr[1] := 2;
    WriteArr[2] := 3;
    WriteArr[3] := 4;

  
    for globalcounter:=0 to 3 do
    begin
         ByteToStr(WriteArr[globalcounter],globaltextbuffer);
         UART1_Write_Text(globaltextbuffer);
    end;

    UART1_Write_Text('test ');
    test(@WriteArr[0]); //WORKS

    for globalcounter:=0 to 3 do
    begin
         ByteToStr(WriteArr[globalcounter],globaltextbuffer);
         UART1_Write_Text(globaltextbuffer);
    end;


    UART1_Write_Text('adr '); //Works,
    byteToStr(@WriteArr[0],globaltextbuffer);
    UART1_Write_text(globaltextbuffer);

    UART1_Write_Text('value '); //Works, returns 2
    byteToStr(WriteArr[0],globaltextbuffer);
    UART1_Write_text(globaltextbuffer);

    UART1_Write_Text('vvp '); //Works, returns 2  vvp = value via pointer
    globalpointer := @WriteArr;
    wordToStr(globalpointer^,globaltextbuffer);
    UART1_Write_text(globaltextbuffer);

    SPI_Write_Buf(0x00,globalpointer,4);  //FAILES, seems that copying of Adresspointer fails

end.
Here is the serial output protocol:

Init 1 2 3 4 2 3 4 5 adr 20 value 2 vvp 2 adr_proc 32 value_proc 32 32 32 32

Help would be much appreciated.

TNX

TurboProgger
Posts: 66
Joined: 04 Oct 2008 18:39
Location: Germany

Re: Access via pointer failes

#2 Post by TurboProgger » 08 Mar 2012 00:10

Hi crestfallen,

you can't pass a pointer to a string to procedure ByteToStr() at its first parameter.
You will at least reach only the first char of the string you are pointing at, unless you increment it's pointer.
You should increment (apointer), until you are reaching the string terminating zero (#0).

Try to build an adequate procedure to handle strings, to which you are pointing at.

regards

crestfallen
Posts: 15
Joined: 01 Jan 2009 14:16

Re: Access via pointer failes

#3 Post by crestfallen » 08 Mar 2012 09:47

Good morning turboprogger,

I hope we don't misunderstood.

I want to pass an array to my procedure. So I assign a pointer to the first field of the array to my procedure...

I'm not pointing to a String (or better to an array of chars).
At least I'm not up to do so...
I'm pointing to an array of byte.

In my outputs I'm dereferencing the pointers to get the values stored at the adresses they are pointing to.

The only location I really want an adress to be outputet is "byteToStr(@WriteArr[0],globaltextbuffer);"
If the adress is an 8 bit value i suppose it should work...

greetings

crestfallen
Posts: 15
Joined: 01 Jan 2009 14:16

Re: Access via pointer failes

#4 Post by crestfallen » 08 Mar 2012 10:20

Research did go on.

I figured out, that the order of the parameters in procedure causes the trouble.

Code: Select all

program Zeigerproblem01;

var WriteArr: array[4] of byte;
var globaltextbuffer: array[8] of char;
var globalcounter: byte;
var globalpointer: ^byte;


procedure Not_Working_Two(firstbyte:byte;apointer: ^byte;);
begin
  apointer^ := 220;
  UART1_Write_text('apointer value');
  WordToStr(apointer^,globaltextbuffer); //Returns Wrong Value
  UART1_Write_text(globaltextbuffer);
end;

procedure Working_Two(apointer: ^byte;firstbyte:byte);
begin
  apointer^ := 110;
  UART1_Write_text('apointer value');
  WordToStr(apointer^,globaltextbuffer); //Returns right Value
  UART1_Write_text(globaltextbuffer);
end;

procedure Working_One(apointer: ^byte);
begin
  apointer^ := 55;
  UART1_Write_text('apointer value');
  WordToStr(apointer^,globaltextbuffer); //Returns right value
  UART1_Write_text(globaltextbuffer);
end;



begin
{ Main program }
    UART1_Init(19200);
    delay_ms(100);
    UART1_Write_text('Init');

    WriteArr[0] := 1;
    WriteArr[1] := 2;
    WriteArr[2] := 3;
    WriteArr[3] := 4;

  
    for globalcounter:=0 to 3 do
    begin
         ByteToStr(WriteArr[globalcounter],globaltextbuffer);
         UART1_Write_Text(globaltextbuffer);
    end;


    Not_Working_Two(0,globalpointer);  //FAILES, seems that copying of Adresspointer fails
    Working_Two(globalpointer,0);  //Works :-)
    Working_One(globalpointer);        //Works 
end.
And Serial Output:

Init 1 2 3 4apointer value 0apointer value 110apointer value 55

User avatar
janko.kaljevic
Posts: 3565
Joined: 16 Jun 2011 13:48

Re: Access via pointer failes

#5 Post by janko.kaljevic » 08 Mar 2012 11:34

Hello,

Yes, this issue was inspected and fixed. It will be in the next release of the compiler.

Please also notice recommendations for getting maximum from SSA in help file.
If the function1 parameters are passed as function2 parameters, then parameter order should remain the same.
Which is in your case. Working_Two function is according recommendations from help file.

Best regards.

Post Reply

Return to “mikroPascal PRO for AVR General”