Why this is not working? Passing function pointer to a function

Status
Not open for further replies.

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,524
Helped
540
Reputation
1,078
Reaction score
524
Trophy points
1,393
Visit site
Activity points
0
Is this correct? Passing function pointer to a function

I am using mikroC PRO PIC. I need to pass address of strcat() function to concat() function. Is my code correct?

mikroC library help file says... (see image)




Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
char msg1[];
char msg2[] = "elektronika";
char *cptr = 0;
 
char *concat(char *(*strCatFuncPtr)(char *, char *), char *str1, char *str2){
        return (*strCatFuncPtr)(str1, str2);
}
 
 
void main(){
 
    while(1){
           msg1[0] = 'm';
           msg1[1] = 'i';
           msg1[2] = 'k';
           msg1[3] = 'r';
           msg1[4] = 'o';
           msg1[5] = '\0';
           cptr = concat(&strcat, &msg1, &msg2);
    }
}



Watch window show these before and after executing concat() function.

 

Attachments

  • strcat.png
    15.6 KB · Views: 179
  • strcat ww.png
    33.8 KB · Views: 173
Last edited:

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

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.

Try defining:

Code:
char msg1[30];


BigDog
 

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.
 

You will need to define the array msg1[] large enough to hold both strings:

Code:
char msg1[30];

Leave array msg2[] as follows:

Code:
char msg2[] = "Elektronika";

Load the array with the string "mikro" as you have done here:

Code:
           msg1[0] = 'm';
           msg1[1] = 'i';
           msg1[2] = 'k';
           msg1[3] = 'r';
           msg1[4] = 'o';
           msg1[5] = '\0';

Or instead, you could use strcat or strcopy to copy the string literal "mikro" to msg1[].

Code:
strcpy(msg1,  "mikro");


BigDog
 

Yes, it worked. It works even if


Code C - [expand]
1
2
3
4
5
6
7
8
9
char msg1[40];
char msg2[] = "Elektronika";
 
void main() {
 
         strcpy(msg1, "mikro");
         strcat(msg1, msg2);
 
}



is used.

 

Attachments

  • mC dbg.png
    22.9 KB · Views: 131

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…