Once the function pointer is declared for a specific function, strcat() in this case:
Code:
char * (*pf)(char *, char *);
The function pointer can assigned the address of the function like so:
Code:
pf = strcat;
The function symbol/name contains the address of the function and is in fact a pointer, much like the array symbol/name contains the address of the first element of the array, use of the address operator (&) is not required.
Code:
char * result;
char * (*pf)(char *, char *);
pf = strcat;
// According to ANSI C (C89), each of the following are acceptable, function the same and achieve the same result
result = strcat(msg1, msg2);
result = (*pf)(msg1, msg2);
result = pf(msg1, msg2);
BigDog
- - - Updated - - -
Reference: mikroC PRO for PIC Manual, Section: Chapter 7 Libraries, Page 577
Prototype char *strcat(char *to, char *from);
Function appends a copy of the string from to the string to, overwriting the null
character at the end of to. Then, a terminating null character is added to the
result. If copying takes place between objects that overlap, the behavior is undefined. to string must have enough space to store the result. The function returns
address of the object pointed to by to.
You failed to define the size of msg1[], then proceed in writing storage beyond its bounds and finally strcat attempts to store the resulting string to pointer msg1 and storage out of bounds.
Ok. I will try declaring global function pointers and assign the address of functions to them. I need to create function pointers for 5 functions. Is it possible to create an array of function pointers? If yes, please show an example.
Initially I had tried
Code C - [expand]
1
2
msg1[30]="mikro";
msg2[40]="Elektronika";
When it did not work, I changed to the code in post #1. I later found that I had to use absolute when declaring variables. The other problem I faced when declared as above is the compiler will not load the variables in the watch window while debugging. I have to remove the element count. If it is declared as
Code C - [expand]
1
2
msg1[]="mikro";
msg2[]="Elektronika";
then only mikroC loads the variables in watch window while debugging.