|
|
View previous topic :: View next topic |
Author |
Message |
bastirn
Joined: 08 Jan 2020 Posts: 17
|
Problem using a function pointer |
Posted: Wed Jan 08, 2020 7:21 am |
|
|
Hello,
I'm trying to implement a shell in a PIC16F1829.
I'm stuck while i try to call a function using function pointer.
You can see a resumé of the problem.
Thanks in advance for any answer,
Best regards,
Bastien NICAUD
Code: |
#include <16F1829.h>
#device PIC16F1829
#device PIC16F1829 *=16
#device ANSI
#DEVICE PASS_STRINGS=IN_RAM
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdarg.h>
#include <stdbool.h>
#include "bnishell.h"
#DEVICE CONST=ROM
#fuses INTRC_IO // Internal OSC
#use delay (clock=32M)
#use rs232(baud=115200, xmit=PIN_C4, rcv=PIN_C5, stream=PC)
int cmd_test(int argc, char ** argv)
{
printf("\n\rtest\n\r");
return SHELL_RET_SUCCESS;
}
void main(){
char *cmd_test_argv;
char **cmd_test_argv2;
cmd_test_argv = 81;
cmd_test_argv2 = &cmd_test_argv ;
int (*toto)(int, char **);
int zz ;
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
//shell_init();
//shell_register(&cmd_test, "permet de tester", "0-1", "test");
toto = cmd_test;
zz = cmd_test(1,cmd_test_argv2); // Works but not what we need
// What we want to do :
zz = (*toto)(1,cmd_test_argv2); // Error return : "No valid assignment made to function pointer" in the .err file
printf("%d",zz);
while (1) {
//shell_task();
}
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19509
|
|
Posted: Wed Jan 08, 2020 9:25 am |
|
|
The problem isn't where you think it is.
It's actually the cmd_test_argv2;
Change the ** declarations to:
int (*toto)(int a, char *b[]);
and the same for the function declaration. CCS does not 'like' pointers
to pointers, but happily accepts pointers to arrays (which give exactly
the same effect).
You need to put a dummy variable name to do this (as I show) |
|
|
bastirn
Joined: 08 Jan 2020 Posts: 17
|
|
Posted: Thu Jan 09, 2020 8:49 am |
|
|
Hello,
Thank you for you answer, now there is no more compilation error,
unfortunately it doesn't work as i want ...
Here is the code that i used :
Code: |
#include <16LF1829.h>
//#device PIC16F1829
#device PIC16LF1829 *=16
#device ANSI
#DEVICE PASS_STRINGS=IN_RAM
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdarg.h>
#include <stdbool.h>
//#include "bnishell.h"
#DEVICE CONST=ROM
#fuses INTRC_IO // Internal OSC
#use delay (clock=32M)
#use rs232(baud=115200, xmit=PIN_C4, rcv=PIN_C5, stream=PC)
int cmd_test(int argc, char * argv[])
{
printf("\n\rtest\n\r");
printf("\n\r %04X \n\r",&argv);
return 1;
}
void main(){
delay_ms(20);
char *cmd_test_argv;
char *cmd_test_argv2[];
int (*toto)(int a, char *b[]);
int zz ;
cmd_test_argv = 81;
cmd_test_argv2 = &cmd_test_argv ;
toto = cmd_test;
//zz = cmd_test(1,cmd_test_argv2); // Works but not what we need
zz = (*toto)(1,cmd_test_argv2);
printf("\n\r %04X \n\r",&cmd_test_argv2);
while (1) {
}
} |
I should have the same adress resulting of this two printf but they are different. Any suggestion ?
Thanks in advance for any answer,
Best regards
Bastien |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19509
|
|
Posted: Thu Jan 09, 2020 9:28 am |
|
|
It won't.
& means 'give the address of'. Called in the subroutine, this gives the
address of the _temporary_ variable holding the value. Not the address
of the original variable. |
|
|
|
|
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
|