question regarding code found on this board

This is a discussion on question regarding code found on this board within the C Programming forums, part of the General Programming Boards category; while reading an old thread I found this bit of code. I was wondering if anyone could explain to me ...

  1. #1
    Unregistered
    Guest

    question regarding code found on this board

    while reading an old thread I found this bit of code. I was wondering if anyone could explain to me the components of the function declaration. I tried using this code, but I got a warning when i tried to compile it, and a link error while attempting to run it. The warning states that Callback is undefined. And the link error states that there is an unresolved external symbol _Callback. I think Prelude is the one who supplied this code, so maybe that's who I need to ask in order to clarify.

    Code:
    int ReadLines(const char *Filename, ReadLineFunc Callback) 
    {
      FILE *fp = fopen(Filename, "r");
      char	hex2[512];
      char *copy;
      
      if(fp == NULL) {
        fprintf(stderr,"Error, file is not found. \n");
        return -1;
      }
    
      /* Read each line and print it out */
      while(fgets(hex2, 512, fp) != NULL) {
        copy = malloc ( strlen ( hex2 ) + 1 );
        if ( copy != NULL ) {
          strcpy ( copy, hex2 );
          Callback(copy);
          free ( copy );
          copy = NULL;
        }
      }
    
      fclose(fp);
    
      return 0;
    }

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Callback() is a function which isn't declared in that piece of code. So the compiler warns that it is an undefined function and the linker gives an error because it cannot find that function. So you need the implementation of the function Callback().

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    In this example, the Callback function is designed to process the string as soon as it's read, then it can be free()'d immediately.

    You can make the function like this to test your code:
    Code:
    void Callback(char *p)
    {
        printf ("CALLBACK: %s\n", p);
    }
    This will simply print the contents of the string.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I think Prelude is the one who supplied this code, so maybe that's who I need to ask in order to clarify.
    The callback argument to that function was apparently there to perform some kind of processing on each line, perhaps a parse of some sort. What it does is call the function given to it as the argument, like so:
    Code:
    void callback ( void )
    {
      (void)puts ( "This is a callback" );
    }
    
    void callfunc ( void (*fp) ( void ) )
    {
      (*fp)();
    }
    
    int main ( void )
    {
      callfunc ( callback );
      return 0;
    }
    The address of callback is passed to callfunc as a function pointer. callfunc then dereferences the pointer to call the function it points to. The code you posted follows the same principle, but it does a little bit more in that it takes an argument.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bubling code question
    By elwad in forum C Programming
    Replies: 2
    Last Post: 04-02-2009, 06:56 PM
  2. Linked List Tutorial sample code question
    By Kalleos in forum C Programming
    Replies: 2
    Last Post: 01-16-2009, 12:20 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  4. C++ code to 68HC11 EVBU board
    By dan047 in forum C++ Programming
    Replies: 1
    Last Post: 10-31-2006, 12:53 PM
  5. Shouldn't either HTML or vB code be enabled in the C++ Board?
    By SilentStrike in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-13-2001, 08:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21