CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Using callback functions with double pointer argument

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
mjm_graco



Joined: 22 Oct 2020
Posts: 4

View user's profile Send private message

PostPosted: Tue Oct 27, 2020 10:24 pm     Reply with quote

Yeah, that original post was all over the place. My apologies.

This combines the concepts of the 2 pieces of code after this one, and gives the compile error.
s_InputBufferWordPtr is a 2D array of words parsed from an input string.
Quote:
*** Error 144 "main.c" Line 24(1,1): No valid assignment made to function pointer 823 from=??0 0 SCR=1241
Code:
#include "18f67k22.h"
#use delay(clock=10M)
#use rs232(baud=9600, UART1, ERRORS)

void TestFunc(int argc, char **argv);

typedef void (*_fptr)(int, char **);

typedef struct
{
    char   cmdString[10];
    _fptr function;
}CommandTableEntry;

CommandTableEntry CommandTable[] = {{"?", TestFunc}};

void main(void)
{
    int   s_InputBufferWordCount;
    char  *s_InputBufferWordPtr[16];
    _fptr temp;
   
    temp = CommandTable[0].function;
    (*temp)(s_InputBufferWordCount, s_InputBufferWordPtr);
}

void TestFunc(int argc, char **argv)
{
    for (int i = 0; i < argc; ++i)
    {
        while(*argv[i])
        {
            printf("%u: %u\r", i, *argv[i]);
            ++argv[i];
        }
    }
}

This compiles and works (although extra code is required to show it works). Shows the callback with a conventional argument:
Code:
#include <18f67k22.h>
#use delay(clock=10M)
#use rs232(baud=9600, UART1, ERRORS)

void TestFunc(int argc);

typedef void (*_fptr)(int);

typedef struct
{
    char   cmdString[10];
    _fptr function;
}CommandTableEntry;

CommandTableEntry CommandTable[] = {{"?", TestFunc}};

void main(void)
{
   int  s_InputBufferWordCount;
   char *s_InputBufferWordPtr[16];
   _fptr temp;
   
    temp = CommandTable[0].function;
    (*temp)(s_InputBufferWordCount);

    while(1);
}

void TestFunc(int argc)
{
    printf("%u\r", argc);
}

This also compiles and works. Shows s_InputBufferWordPtr being passed to a function, but without the callback.
Code:
#include <18f67k22.h>
#use delay(clock=10M)
#use rs232(baud=9600, UART1, ERRORS)

void TestFunc(int argc, char **argv);

void main(void)
{
   int  s_InputBufferWordCount;
   char *s_InputBufferWordPtr[16];
   
    TestFunc(s_InputBufferWordCount, s_InputBufferWordPtr);
    while(1);
}

void TestFunc(int argc, char **argv)
{
    for (int i = 0; i < argc; ++i)
    {
        while(*argv[i])
        {
            printf("%u: %u\r", i, *argv[i]);
            ++argv[i];
        }
    }
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19249

View user's profile Send private message

PostPosted: Wed Oct 28, 2020 6:47 am     Reply with quote

Generally, CCS is rather restrictive with regards to handling pointer to
pointers and similar constructs. I've found that usually the best way to
avoid problems is to instead hand all values to functions as integers
(in the case of the PIC18, normally int16), and then convert these
values back to the required pointer where used.
So a fiddle like:
Code:

#include <18f67k22.h>
#use delay(clock=10M)
#use rs232(baud=9600, UART1, ERRORS)

void TestFunc(int argc, int16 argv);

typedef void (*_fptr)(int, int16);

typedef struct
{
    char   cmdString[10];
    _fptr function;
}CommandTableEntry;

CommandTableEntry CommandTable[] = {{"?", TestFunc}};
char string[] = "Test message";

void main(void)
{
   int  s_InputBufferWordCount=1;
   char *s_InputBufferWordPtr[16];
   _fptr temp;
   s_inputBufferWordPtr[0]=string;
   s_inputBufferWordPtr[1]=(char *)0;
   
    temp = CommandTable[0].function;
    (*temp)(s_InputBufferWordCount, (int16)s_InputBufferWordPtr );

    while(TRUE);
}

void TestFunc(int argc, int16 temp)
{
    char ** argv;
    argv=temp;
    printf("%u\r", argc);
    for (int i = 0; i < argc; ++i)
    {
        while(*argv[i])
        {
            printf("%u: %u\r", i, *argv[i]);
            ++argv[i];
        }
    }     
}


Ought to work (possibly...).
mjm_graco



Joined: 22 Oct 2020
Posts: 4

View user's profile Send private message

PostPosted: Wed Oct 28, 2020 7:29 am     Reply with quote

That worked. Thanks for the help - 'tis appreciated.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group