Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

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
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)

102109d1392273845-strcat.png



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.

102110d1392274246-strcat-ww.png
 

Attachments

  • strcat.png
    strcat.png
    15.6 KB · Views: 155
  • strcat ww.png
    strcat ww.png
    33.8 KB · Views: 147
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
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.

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, you are right. It is better to use strcpy() function to load msg1[].
 

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.

102189d1392446044-mc-dbg.png
 

Attachments

  • mC dbg.png
    mC dbg.png
    22.9 KB · Views: 112

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top