Thread: Having issues implementing a linked list ADT with a function pointer.

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    1

    Having issues implementing a linked list ADT with a function pointer.

    So I have been working on this without much cause for celebration.

    I have implemented the creation of the list, the push and pop aspects of it and the iterator for which you traverse the list. However there is one aspect that aludes me which is this function pointer that I am having trouble "baking into" my list.

    The function pointer in question is:

    typedef int (*cmpfunc_t)(void *, void *); specifically I have never encountered one of these before and googling it yielded little to no answers for how to proceed.

    My code in its entirety: common.h * GitHub


    Any help would be appreciated!

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    I can't compile your program:
    * common.h is missing some lines from the top
    * list.c is missing at least one line from the bottom.
    * list.h is missing entirely.
    * there's no main.

    You should also maximize your compiler's warning level to help catch unused variables and the like. For example, in list_popfirst, you set tmp but never use it.

    In list_create, the given comparison function is supposed to be stored in the list structure. list_contains and list_sort are supposed to use this sort function.

    It is strange and presumably wrong that you are creating a new node in list_contains and then comparing addresses. You need to compare the "elements" using the comparison function. BTW, "elements" is a weird name for what is essentially the "data" of the node.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Declaring compatible functions, and calling them via function pointers.
    Code:
    #include<stdio.h>
    
    typedef int (*cmpfunc_t)(void *, void *); 
    
    int foo ( void *a, void *b ) {
      int *pa = a;
      int *pb = b;
      return *pa - *pb;
    }
    
    int main()
    {
      int a = 7, b = 4;
      cmpfunc_t fn = foo;
      int result = fn(&a,&b);
      printf("a=%d, b=%d, result=%d\n", a, b, result);
      return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation Fault -- Stack implementing a Linked List
    By jackalclone1 in forum C Programming
    Replies: 10
    Last Post: 02-15-2014, 12:44 AM
  2. Linked List Issues
    By Sorinx in forum C Programming
    Replies: 13
    Last Post: 12-10-2012, 02:19 PM
  3. Replies: 4
    Last Post: 05-31-2011, 11:34 AM
  4. Implementing a linked list, some problems
    By EvBladeRunnervE in forum C++ Programming
    Replies: 7
    Last Post: 12-12-2003, 09:07 AM
  5. implementing graph using linked list
    By Tim in forum C Programming
    Replies: 1
    Last Post: 11-20-2001, 02:00 AM

Tags for this Thread