| View previous topic :: View next topic |
| Author |
Message |
BiB
Joined: 27 Feb 2004 Posts: 36 Location: France
|
Posted: Fri Jun 10, 2005 11:09 pm Post subject: Problem with pointers on functions |
|
|
Hi,
I want to use pointer variables who points to functions.
I did this piece of code, compile well, but didn't run it correctly :s
| Code: |
#include <tamtypes.h>
#include <debug.h>
typedef void (*proc)(int);
void test(int integer) {
scr_printf("%d\n",integer);
}
int main() {
init_scr();
proc fct = &test;
fct(2);
return 0;
}
|
I expected the value 2 displayed but a strange number (1047008) appared in loop on the screen.
I don't remember exactly but i did such a program in C for linux and this kind of program worked.
Any idea ???
Thx |
|
| Back to top |
|
 |
cheriff Regular
Joined: 23 Jun 2004 Posts: 262 Location: Sydney.au
|
Posted: Fri Jun 10, 2005 11:15 pm Post subject: |
|
|
You shouldn't need to take the address of the function. Try: _________________ Damn, I need a decent signature! |
|
| Back to top |
|
 |
evilo

Joined: 22 Apr 2004 Posts: 230
|
Posted: Fri Jun 10, 2005 11:19 pm Post subject: |
|
|
I would personnally do this :
void (*my pointer_fct)(void);
void myfct()
{
my fancy code...
}
my pointer_fct = myfct;
and then
pointer_fct (); |
|
| Back to top |
|
 |
BiB
Joined: 27 Feb 2004 Posts: 36 Location: France
|
Posted: Fri Jun 10, 2005 11:58 pm Post subject: |
|
|
Same result when i don't take the address of the function.
evilo : i also tried your solution but the result is the same as above |
|
| Back to top |
|
 |
evilo

Joined: 22 Apr 2004 Posts: 230
|
Posted: Sat Jun 11, 2005 12:06 am Post subject: |
|
|
strange...
I have it working in some stuff I made for the ps2 ! |
|
| Back to top |
|
 |
BiB
Joined: 27 Feb 2004 Posts: 36 Location: France
|
Posted: Sat Jun 11, 2005 12:08 am Post subject: |
|
|
| i didn"t say that i tried my compiled elf on PCSX, not directly on the PS2 (which is .... how to say that.... quite dead for the moment !!! ) |
|
| Back to top |
|
 |
BiB
Joined: 27 Feb 2004 Posts: 36 Location: France
|
Posted: Sat Jun 11, 2005 12:24 am Post subject: |
|
|
Hmmm... it seems to be a "bug" or something strange from the scr_printf function.
When i execute this piece of code :
| Code: | int val = 0;
void test(int integer) {
val = integer;
//scr_printf("integer = %d\n",integer);
}
int main() {
init_scr();
void (*proc)(int) = test;
scr_printf("Before : val = %d\n",val);
proc(2);
scr_printf("After : val = %d\n",val);
return 0;
} |
The result is correct :
Before : val = 0
After : val = 2
But when I replace my test function code by 'scr_printf("%d\n",integer), a wrong result appared in loop.
So i can execute correctly my prog but can't debug it :( |
|
| Back to top |
|
 |
dlanor
Joined: 28 Oct 2004 Posts: 269 Location: Stockholm, Sweden
|
Posted: Sun Jun 12, 2005 7:17 am Post subject: |
|
|
| BiB wrote: | Hmmm... it seems to be a "bug" or something strange from the scr_printf function.
| Strange perhaps, but no bug. The problem is that your pointer doesn't match the function type.
You first declared the pointer as: void (*proc)(int) = test;
Which I presume you changed to: void (*proc)(int) = scr_printf;
(As that matches your earliest example.)
That would be fine if scr_printf had a matching prototype, but according to debug.h that is declared as:
void scr_printf(const char *, ...) __attribute__((format(printf,1,2)));
As you can see the number of arguments is variable (depends on the string argument), and the main argument is not an 'int' but a 'char *'. In fact you should have received a warning about bad type match when you compiled this, but perhaps you had disabled warnings...
One obvious solution to this problem is to make an intermediate function which matches the 'void (*proc)(int)' type, and call scr_printf directly from that function. This intermediate function can then be used for pointers as you intended.
Best regards: dlanor |
|
| Back to top |
|
 |
urchin
Joined: 02 Jun 2005 Posts: 121
|
Posted: Sun Jun 12, 2005 5:43 pm Post subject: |
|
|
| reading BiB's last post, I understood it to mean that when the scr_print line is uncommented in the test function the result is different (the line is commented out in the example given) |
|
| Back to top |
|
 |
dlanor
Joined: 28 Oct 2004 Posts: 269 Location: Stockholm, Sweden
|
Posted: Mon Jun 13, 2005 1:32 am Post subject: |
|
|
| urchin wrote: | | reading BiB's last post, I understood it to mean that when the scr_print line is uncommented in the test function the result is different (the line is commented out in the example given) |
You may be right, but I assumed that he'd tried to pass the address of scr_printf directly to a pointer, since that is one of the things that could result in 'strange numbers' being printed, though they were never passed as arguments.
When I do what you say, it works without flaw, so he must have done something different (unless he has a screwed-up ps2dev setup). But of course, he mentioned that he's testing on an emulator rather than a real PS2, so that could also be messing with his results.
Best regards: dlanor |
|
| Back to top |
|
 |
BiB
Joined: 27 Feb 2004 Posts: 36 Location: France
|
Posted: Mon Jun 13, 2005 5:23 pm Post subject: |
|
|
You're right urchin. I never tried to pass the adress of scr_printf directly to a pointer.
I pass the adress of a function which contains a scr_printf instruction inside. That's why i find this behaviour very strange. |
|
| Back to top |
|
 |
urchin
Joined: 02 Jun 2005 Posts: 121
|
Posted: Mon Jun 13, 2005 10:58 pm Post subject: |
|
|
| You should try your code on a real PS2, as using a PS2 emulator adds a monstrously huge variable into the equation. |
|
| Back to top |
|
 |
BiB
Joined: 27 Feb 2004 Posts: 36 Location: France
|
Posted: Tue Jun 14, 2005 6:29 pm Post subject: |
|
|
| I'll try when i repair my ps2 :S |
|
| Back to top |
|
 |
|