c struct passing itself as an argument into a pointer function

user2419860

I am trying to create a structure with nested functions, that passes the structure itself as an argument into the nested function for simplification of calling functions. The code will be run on a Pic 24f series mcu using the xc16 compiler. Also, I know the count function is redundant but I think it illustrates my point. For example:

Structure:

typedef struct
{
    uchar Fifo[UART1_RX_MAX_BUFFER_SIZE];
    uchar FifoIndex = 0;
    uchar FifoCount = 0;
    uchar FifoOverrunFlag = FALSE;
    uchar (*CountPointer);
    uchar (*PutPointer);
}Fifo;

Functions:

// Returns: The number of items in the Fifo
uchar FifoGetCount(Fifo *fifo)
{
    return fifo->FifoCount;
}

// Summary: Adds data to the end of the Fifo
// Parameters:  
//          data: Data to be added to the end of the Fifo
// Returns: True (1) if the data was successfully added
//      False (0) if the data was not successfully added
//          as a result of the Fifo being full
uchar FifoPut(Fifo *fifo, uchar data)
{
    if (fifo->FifoCount > FIFO_MAX_SIZE)
    {
        fifo->FifoOverrunFlag = TRUE;
        return FALSE;
    }
    uint putIndex = fifo->FifoGetPutIndex();
    fifo->Fifo[putIndex] = data;
    fifo->FifoCount++;
    return TRUE;
}

Main:

Fifo fifo1;

int main()
{
    fifo1.CountPointer = FifoGetCount(fifo1);
    fifo1.PutPointer = FifoPut(fifo1, uchar data);

    // Intended Usage
    uchar myCount = fifo1.FifoGetCount();
    uchar myData = 1;
    fifo1.FifoPut(myData);
}
Jonathan Leffler

Note that uchar (*PutPointer); is just a funny way of writing uchar *PutPointer;. To create a function pointer, you'd have to write: uchar (*PutPointer)(…); where the should be replaced by a specification of the types of the arguments — a prototype.

Since you have a function uchar FifoPut(Fifo *fifo, uchar data), it seems likely that you should be writing:

typedef struct Fifo Fifo;

struct Fifo
{
    uchar Fifo[UART1_RX_MAX_BUFFER_SIZE];
    uchar FifoIndex = 0;
    uchar FifoCount = 0;
    uchar FifoOverrunFlag = FALSE;
    uchar (*CountPointer)(Fifo *fifo);
    uchar (*PutPointer)(Fifo *fifo, uchar data);
};

And then, in main(), you could write:

fifo1.CountPointer = FifoGetCount;
fifo1.PutPointer = FifoPut;

This assigns the function pointers FifoGetCount and FifiPut to the elements of the struct. The code you had assigned a uchar value generated by calling the function once to a uchar *. This should have been generating compiler warnings. If it was, you should have said so in your question. If it wasn't, you need turn on relevant compiler warnings, or get a better compiler.

You could then use the function as:

uchar myCount = fifo1.FifoGetCount(&fifo1);
uchar myData = 1;
fifo1.FifoPut(&fifo1, myData);

Note that you have to explicitly pass the address of the structure as the first argument to the functions; the C compiler is not going to do that for you by magic. If you want that done implicitly, the language you're looking for is called C++.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

passing function pointer in c with a pointer argument

From Dev

c++ passing function itself as argument

From Dev

Passing swift struct pointer to C function

From Dev

Passing a pointer to a char array as an argument to a function - C

From Dev

Passing a pointer to a char array as an argument to a function - C

From Dev

function with a struct array pointer as an argument, C language

From Dev

Cannot pass struct pointer as function argument in C

From Dev

advantage of passing pointer to a struct as argument?

From Dev

Passing a pointer to struct as an argument in Julia

From Dev

Passing an argument to function pointer

From Dev

Passing pointer to a member of a struct to a function

From Dev

Passing char pointer as argument to function

From Dev

A weird error when passing a struct pointer to a function in C

From Dev

C const pointer to const struct array as function argument

From Dev

passing array (not pointer) to a struct in c

From Dev

C - Passing pointer to function

From Dev

Passing a pointer to a member function as argument for a void* function

From Dev

Segmentation fault passing struct pointer to function

From Dev

Passing an argument in the function to turn struct values into an array

From Dev

Updating pointer address in function, when passed as an argument. [ passing pointer as reference in C]

From Dev

passing a pointer to a class method as a function argument

From Dev

Passing a "pointer to a virtual function" as argument in Python

From Dev

Passing pointer to member function parameter into template argument

From Dev

Passing a "pointer to a virtual function" as argument in Python

From Dev

passing a pointer to a class method as a function argument

From Dev

passing a pointer argument to a function in other file

From Dev

Modifying the argument of a function without passing a pointer

From Dev

Passing a Pointer as an Argument in C/C++?

From Dev

C - passing struct pointer terminates the program? How to

Related Related

  1. 1

    passing function pointer in c with a pointer argument

  2. 2

    c++ passing function itself as argument

  3. 3

    Passing swift struct pointer to C function

  4. 4

    Passing a pointer to a char array as an argument to a function - C

  5. 5

    Passing a pointer to a char array as an argument to a function - C

  6. 6

    function with a struct array pointer as an argument, C language

  7. 7

    Cannot pass struct pointer as function argument in C

  8. 8

    advantage of passing pointer to a struct as argument?

  9. 9

    Passing a pointer to struct as an argument in Julia

  10. 10

    Passing an argument to function pointer

  11. 11

    Passing pointer to a member of a struct to a function

  12. 12

    Passing char pointer as argument to function

  13. 13

    A weird error when passing a struct pointer to a function in C

  14. 14

    C const pointer to const struct array as function argument

  15. 15

    passing array (not pointer) to a struct in c

  16. 16

    C - Passing pointer to function

  17. 17

    Passing a pointer to a member function as argument for a void* function

  18. 18

    Segmentation fault passing struct pointer to function

  19. 19

    Passing an argument in the function to turn struct values into an array

  20. 20

    Updating pointer address in function, when passed as an argument. [ passing pointer as reference in C]

  21. 21

    passing a pointer to a class method as a function argument

  22. 22

    Passing a "pointer to a virtual function" as argument in Python

  23. 23

    Passing pointer to member function parameter into template argument

  24. 24

    Passing a "pointer to a virtual function" as argument in Python

  25. 25

    passing a pointer to a class method as a function argument

  26. 26

    passing a pointer argument to a function in other file

  27. 27

    Modifying the argument of a function without passing a pointer

  28. 28

    Passing a Pointer as an Argument in C/C++?

  29. 29

    C - passing struct pointer terminates the program? How to

HotTag

Archive