Thread: List of functions

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    12

    List of functions

    Hello,

    I have functions which return data from the database table, eg:
    Code:
    int get_int(); 
    char *get_text(); 
    double get_real();
    Now, I wonder if it is possible to accommodate them into some sort of list, that I could do smth like this:

    Code:
    for (int i = 0; i < table_column_count; i++)
    {
        display_data(i, function_list[i]);
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes. And also, no.
    You can use function pointers. That way you can store the addresses to the function in a list and call them later. But they must share the same signature. Otherwise you have to figure out the correct signature for the function later on and do casts. Nasty stuff, and error prone.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    12
    Yes, I can do this or I can wrap those functions in void functions and then they would share the same signature, but I need more elegant way (can use asm, if needed).

    I still don't understand what prohibits C from doing this (it's not part of a standard, but... is it hard to implement for compiler or what?).

    Currently, I do some sort of getting datatype of column, storing it into array, switching to required function according to an array value. Lots of time wasted, because there are lots of columns and lots of rows.

    Until I'll fix this, I can't go further.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The short answer is not because it's difficult, but because C wasn't designed for it. It's not the goal of the language, so to speak.
    I don't have a solution yet. I must think on this, and I don't think I have time at the moment.
    But consider a higher level language, such as C++ if you still need to stay close to the hardware. It will simplify things a bit, albeit perhaps not completely fix.

    Now then, the question is: why do you need this? It obviously is tricky to implement and when it's tricky to implement, often it is a sign that you're going about something the wrong way.
    Last edited by Elysia; 02-10-2010 at 05:20 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    12
    I can't use C++, because compiler might not be available for that hardware. I would rather stay in ANSI C domain and use assembler language instead (should I?)

    I use SQLite database ( http://www.sqlite.org/c3ref/column_blob.html ). It's easy to implement when you do not care about the speed, but when you have thousands of records each if matters.
    Last edited by Alh; 02-10-2010 at 05:36 AM.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't think machine language will help you here...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Yes you can. Although you're going around it the wrong way.

    Code:
    zac@neux:~ (0) $ gcc -o example code.c
    zac@neux:~ (0) $ ./example 
    item[0] = String value
    item[1] = 64
    item[2] = 3.141000
    zac@neux:~ (0) $ cat code.c 
    #include <stdio.h>
    #include <stdlib.h>
    
    enum types_tag
    {
       TYPE_STRING = 0,
       TYPE_REAL,
       TYPE_INT
    };
    
    struct database_tag
    {
       enum types_tag type;     /* type of value held */
       union
       {
          int integer;
          double real;
          char * stringValue;
       } value;
    };
    
    
    const char * stringize_value(const struct database_tag * db)
    {
       static char buffer[256] = {0};      /* large enough for stringized real or int */
       char * ret = NULL;
    
       switch(db->type)
       {
          case TYPE_INT:
             sprintf(buffer, "%d", db->value.integer);
             ret = buffer;
          break;
    
          case TYPE_REAL:
             sprintf(buffer, "%f", db->value.real);
             ret = buffer;
          break;
    
          case TYPE_STRING:
             ret = db->value.stringValue;
          break;
    
          /* unknown type */
          default:
             ret = "unknown type";
       }
    
       return ret;
    }
    
    int main(void)
    {
       size_t i = 0;
       struct database_tag items[] = {  {TYPE_STRING, 0},
                                        {TYPE_INT, 0},
                                        {TYPE_REAL, 0}};
       items[0].value.stringValue = "String value";
       items[1].value.integer = 64;
       items[2].value.real = 3.141;
    
       for(i = 0; i < (sizeof items / sizeof items[0]); ++i)
       {
          printf("item[%lu] = %s\n", (unsigned long int) i, stringize_value(&items[i]));
       }
    
       return 0;
    }

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    12
    In asm I'd put params on the stack and would fetch them from there.

    Thanks for example, but you still use switch. In my case it would be switch for each cell in the table.
    Last edited by Alh; 02-10-2010 at 05:54 AM.

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by Alh View Post
    In asm I'd put params on the stack and would fetch them from there.
    ... as what size/type?

  10. #10
    Registered User
    Join Date
    Feb 2010
    Posts
    12
    The size of data would depend on the arch (4 bytes on x86). The type depends on how your asm subprogram will interpret data.

  11. #11
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by Alh View Post
    The size of data would depend on the arch (4 bytes on x86). The type depends on how your asm subprogram will interpret data.
    If that's all you think is to it, then I certainly wouldn't recommend it. It wouldn't work anyway. How would you know what type to interpret the argument at when you pop off at the caller? Or whatever calling convention your compiler/OS is using (yet another consideration). And you've already made it compiler specific, assembler specific and machine specific. All for a simple return value, pfft!

    Is there anything wrong with the idea I posted?

  12. #12
    Registered User
    Join Date
    Feb 2010
    Posts
    12
    One can define calling convention. I've already looked up: __cdecl.

    Data output function knows how to interpret data. I pass column data type as one of the parameters.

    40 columns & > 1000 rows equals to > 40000 cells and there is a switch for each of it. And there are many more than 1000 rows. I need speed, not portability. I would prefer both, but if it's not possible, then speed is my main criteria.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If the function knows what data to expect, why not just return void* and be done with it!?!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Feb 2010
    Posts
    12
    Because SQLite API return types are not void* (char*, int, double). I can not return a pointer when it's an int.
    Last edited by Alh; 02-10-2010 at 07:49 AM.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Alh
    Thanks for example, but you still use switch. In my case it would be switch for each cell in the table.
    As in the switch will be tested for each cell in the table?
    Last edited by laserlight; 02-10-2010 at 08:10 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  2. Recursion Revisited again, and again!
    By clegs in forum C++ Programming
    Replies: 93
    Last Post: 12-08-2007, 08:02 PM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM