pointer - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

pointer

When I try to add a constant to a pointer the compiler gives me an error.

{xample}

r:mypointer;

...

(r+3)^.record.data:=8;

...

I could do this same thing easily in C, but I need to do something like this in Pascal.Any work around or solution would be excellent.


Comments

  • : When I try to add a constant to a pointer the compiler gives me an error.

    : {xample}

    : r:mypointer;

    : ...

    : (r+3)^.record.data:=8;

    : ...

    : I could do this same thing easily in C, but I need to do something like this in Pascal.Any work around or solution would be excellent.


    Yes! Pascal is a real programming language,

    and you cannot do this!

    pointer is a 4-bytes variable

    containing word (selector/segment)+offset.

    you can try to increasing the offset..

    how ever you should do it a bit different in

    Pascal. Get Free Pascal if you want to

    get pointer increment+32bit compiler

    from: www.brain.uni-freiburg.de/~klaus/fpc


  • : When I try to add a constant to a pointer the compiler gives me an error.

    : {xample}

    : r:mypointer;

    : ...

    : (r+3)^.record.data:=8;

    : ...

    : I could do this same thing easily in C, but I need to do something like this in Pascal.Any work around or solution would be excellent.


    The problem is that you are using different types and I guess that's the error.

    One solution is to work with arrays:

    r[3]^,record.data := 8;

    I think this works (I'm not sure though)

    In borland pascal, there is a subroutine GetMem (?) or something like that that converts an address to an integer. That would be a solution







  • : : When I try to add a constant to a pointer the compiler gives me an error.

    : : {xample}

    : : r:mypointer;

    : : ...

    : : (r+3)^.record.data:=8;

    : : ...

    : : I could do this same thing easily in C, but I need to do something like this in Pascal.Any work around or solution would be excellent.


    : The problem is that you are using different types and I guess that's the error.

    : One solution is to work with arrays:

    : r[3]^,record.data := 8;

    : I think this works (I'm not sure though)

    : In borland pascal, there is a subroutine GetMem (?) or something like that that converts an address to an integer. That would be a solution


    I could not get the array indexing to work on pointers. Also GetMem just allocates memory of a certain size to a variable. Thanks anyways.




  • : : When I try to add a constant to a pointer the compiler gives me an error.

    : : {xample}

    : : r:mypointer;

    : : ...

    : : (r+3)^.record.data:=8;

    : : ...

    : : I could do this same thing easily in C, but I need to do something like this in Pascal.Any work around or solution would be excellent.


    : The problem is that you are using different types and I guess that's the error.

    : One solution is to work with arrays:

    : r[3]^,record.data := 8;

    : I think this works (I'm not sure though)

    : In borland pascal, there is a subroutine GetMem (?) or something like that that converts an address to an integer. That would be a solution


    I could not get the array indexing to work on pointers. Also GetMem just allocates memory of a certain size to a variable. Thanks anyways.




  • : : When I try to add a constant to a pointer the compiler gives me an error.

    : : {xample}

    : : r:mypointer;

    : : ...

    : : (r+3)^.record.data:=8;

    : : ...

    : : I could do this same thing easily in C, but I need to do something like this in Pascal.Any work around or solution would be excellent.


    : Yes! Pascal is a real programming language,

    : and you cannot do this!

    : pointer is a 4-bytes variable

    : containing word (selector/segment)+offset.

    : you can try to increasing the offset..

    : how ever you should do it a bit different in

    : Pascal. Get Free Pascal if you want to

    : get pointer increment+32bit compiler

    : from: www.brain.uni-freiburg.de/~klaus/fpc


    I also hold the belief that Pascal is a real programming language...its' just not as flexible as C. All I wanted was a way to interate through pointers using a counter or something. He my problem below.

    {Node test program by j. sotac}

    program pointer_test;

    uses crt,dos;

    type

    nodeptr = ^work_file_ptr; {pointer of addr_node_workfile type}


    string20 = string[20];

    string15 = string[15];

    string10 = string[10];

    string2 = string[2];

    string1 = string[1];

    string3 = string[3];

    string6 = string[6];

    string4 = string[4];




    textfile = file of char; {files will be read chracter by character}


    work_file = record

    testnumber:string6; {assume <= Test99 (could be extended)}<br>
    stId:string4; {assume ID string <= 99999}<br>
    stName:string20; {assume name <= 20 characters}<br>
    stStatus:string6; {assume 'failed' or 'passed' string}

    stScore:string3; {assume no score higher than 999}

    numques:string3;{assume no more than 999 questions}

    end; {end record definition}


    work_file_ptr = record

    WORKRECORD:WORK_FILE;

    prev:nodeptr;

    next:nodeptr;

    end;{end of pointer workfile type}




    workfp = file of work_file; {declare file that will hold workfiles}


    var {-Only 5 bytes needed at start up globally-}


    ptrHead,ptrTail:nodeptr; {points to the first node and list node rep.}

    s:string4;

    {NOTE START POINT ALWAYS POINTS TO FIRST NODE

    START POINTER prev field is always nul }

    const

    cr = #13; {ASCII code for the control chracter carriage return}

    vertbar = #124; {ASCII code for vertical bar(pipe)}




    {*************************************************************************}

    {*************START FUNCTION AND PROCEDURE DEFINITIONS********************}

    {*************************************************************************}

    BEGIN

    clrscr;


    new(ptrHead);

    new(ptrTail);


    ptrHead^.next:=nil;

    ptrHead^.prev:=nil;

    ptrTail^.next:=nil;

    ptrTail^.prev:=nil;




    ptrHead^.next:=ptrTail;

    ptrTail^.prev:=ptrHead;

    {NONE OF THESE BELOW WORK MASTER I.}

    ptrTail[1]^.workrecord.stId:='4302';

    ptrHead[0]^.workrecord.stId:='2433';

    s:=(ptrHead+1)^.workrecord.stId;

    {variable 's' should equal '4301' (assume ptrTail is placed after ptrHead)}


    END.


  • *huge snip*


    Could be me, but I have NO idea WHAT you are trying to accomplish? What address do you want to access?


    Am I correct in assuming that by using PtrHead+1 you are trying to shift all contents of your record one byte to the left?


    Point is Pascal is not very flexibel here, workarounds will have to be used here (forced typecasting/typeoverlaying, stuff like that).




  • : *huge snip*


    *little snip*


    I apologize for may inability to express the problem in an efficient manner. The overall point was to find out if there was a back door way to overcome the limitations of the pascal language. In the C/C++ languages, constants can be added to pointer types. This allows for me to go to the next element, if this is a linked list pointer. Here is an example

    //C style

    struct linked_list

    {

    //...put other structure members here...

    int data;

    linked_list *next_list;

    }

    // Now declare and allocate memory for

    //linked list

    ...

    linked_list *Head;

    linked_list *First;

    ...

    Head = First; //Remember beginning of linked list

    First.data = 5

    //Alloc memory for next record in list

    First.next_list = malloc(sizeof(linked_list));

    First = First.next_list;

    First.data = 10

    The expression '*(Head+1).data' should = 10

    As is clear, the constant '1' was added to the pointer 'Head'.This causes 'Head' to point to the next element in the list.


    If this confuses you more, be so kind as to request additional data.






Sign In or Register to comment.