Thread: function pointers and header files and such

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    16

    function pointers and header files and such

    Okay, so I'm a rather green c programmer here. I need some help with header files. What goes where etc. As I think that's the reason this won't compile. I'll give you the code and hopefully you'll understand what it is I'm trying to do here. Basically I've been given an assignment to used function pointers, but no real direction other than "use function pointers for datatype functions". So this was my idea, wrap the handing of records into another struct with function pointers that do all the work of managing array operations etc.

    data.h
    Code:
    typedef struct record
    {
        char name[40];
        int logins;
        int time;
    } record;
    
    
    typedef struct data
    {
        int size;
        int count = 0;
        record * arr;
    
        void (*init) (data *,int);
        init = &initFunc;
    
    } data;
    
    void initFunc (data *,int);
    data.c
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    #include "data.h"
    
    void initFunc (data *foo, int size)
    {
        foo.arr = (record *) malloc(sizeof(record)*size);
        foo.size = size;
    }
    in main.c
    Code:
    .
    .
    .
    data foo;
    foo.init(&foo,8);
    .
    .
    .

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
        void (*init) (data *,int);
        init = &initFunc;
    That's the part that does not compile is it not? You'd really have to assign yrinstanceofthestruct.init = &initfunc every time you make an instance of yr struct to make that work (annoying). It doesn't make all that much sense to do this in your situation, it would be easier to just say init(&foo, 8) (like passing this pointer in c++).

    Something where a function pointer as a member might be useful is if you had some data pointed to by another member void * that needed to be sorted, and then you would sort it using the function assigned to the function pointer. This is the premise of the library function qsort basically.

    If you can think of a situation where it might be useful to be able to customize what function is being called to respond to whatever is happening. A function which reads data from a file might want a function pointer to a function which would be processing the data that is being read from the file in a custom manner.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    16
    Guess I should have been more specific, apologies.

    Here's the compiler output:
    Code:
    data.h:12: warning: no semicolon at end of struct or union
    data.h:12: error: syntax error before '=' token
    data.h:15: error: syntax error before '*' token
    data.h:17: warning: type defaults to `int' in declaration of `data'
    data.h:17: warning: data definition has no type or storage class
    data.h:19: error: syntax error before '*' token
    last_sort.c: In function `main':
    last_sort.c:34: error: syntax error before "foo"
    last_sort.c:35: error: `foo' undeclared (first use in this function)
    last_sort.c:35: error: (Each undeclared identifier is reported only once
    last_sort.c:35: error: for each function it appears in.)
    This is the reason I thought it was just how I had the files set up or something. But you didn't mention anything wrong with how I structured my code.

    In any case, I know the use of function pointers this way is useless but that's how the assignment reads. I have to use function pointers for all datatype functions other than init type stuff.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    typedef struct data
    {
        int size;
        int count = 0;
        record * arr;
    
        void (*init) (data *,int);
        init = &initFunc;
    
    } data;
    Where it's referenced. Where it thenceforth becomes available.

    Consider typdefing it first, or using what is available at the time (struct data).
    http://c-faq.com/decl/selfrefstruct.html
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    16
    I did change that, but it didn't help. The compiler is still giving the same errors. Line 14 being "int count = 0;"

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Because struct data is a type, not yet an object. You cannot initialize what doesn't exist. Get rid of the = 0 in the declaration of the type -- put it in the object definition.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    16
    Oh gee, that makes sense. Now I feel rather stupid. I got it to compile now. Thanks.

Popular pages Recent additions subscribe to a feed