Thread: Generic Pointer Arithmetic

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    43

    Post Generic Pointer Arithmetic

    I'm trying to implement a generic function pointer which does basic arithmetic (basic linked-list traversal) using a type based on the argument that the function received.

    So to reverse position in a linked list:

    Code:
    void *reverse(void *init, unsigned int N)
    {
        void *ptr = init;
        unsigned int i;
    
        for(i = 0; i < N; i++)
            ptr = ptr->prev;
           
        return ptr;
    }
    As the first argument this function takes an argument init which will be a specific list element of a certain type -- this is established when reverse is called. The function then goes back N elements from init and is supposed to return the resulting pointer (which ought to be of the type of init that was originally passed).

    I am missing something here. The compiler wants me to cast ptr. I am curious why this is not working.

    Should I do (?):

    Code:
    void *reverse(void *init, unsigned int N)
    {
        void *ptr = init;
            
        *((char *)ptr) = *(char *)ptr - N * sizeof(*(char *)ptr);
            
        return ptr;
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    You must cast a void pointer to dereference the pointer. So when you get to the loop where you want to move using the prev link, there is a problem.

    You should use your linked list type to solve the problem. Normally to design a reusable linked list in C, you have a type like this:
    Code:
    struct list_type
    {
       struct list_type *prev, *next;
       void *data;
    };
    This way functions that process the list proper can be separated from functions that process the data (which changes with each reuse). The rare places the two would meet, such as a linked list search or sort function, you can implement a callback with a function pointer parameter in the linked list processing function.

  3. #3
    Registered User
    Join Date
    Jan 2016
    Posts
    43
    OK thanks for this explanation. The key was to abstract the data pointer to another level up so that it could be used for more than one type of list data structure. This is what I was after. Namely a linked list of arbitrary nodes as opposed to a linked list of specific data structure nodes. This way one implementation can be used for multiple types of lists all with different data structures. The code is now a little more complicated as it now takes two instructions to access the data -- first the node, and second the data at the node. Previously it was the data which was also the node.

    Many thanks for your helpful reply.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer arithmetic
    By GiForce in forum C Programming
    Replies: 5
    Last Post: 10-29-2016, 06:31 AM
  2. Pointer arithmetic
    By kelleannmccan in forum C++ Programming
    Replies: 15
    Last Post: 12-07-2011, 11:32 PM
  3. Pointer arithmetic
    By _arjun in forum C Programming
    Replies: 1
    Last Post: 09-20-2011, 11:06 AM
  4. Pointer arithmetic
    By depietro in forum C Programming
    Replies: 13
    Last Post: 03-29-2007, 06:03 AM
  5. pointer arithmetic
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-04-2001, 06:45 PM

Tags for this Thread