Function Pointer ! How and Why ?

K

Kathir

In C,

I have a doubt about Function pointers.

Lets say ,

typedef void (*test_audit_proc_t)( int *t, int index, int instnb);

typedef struct a_index
{
test_audit_proc_t test_mo_proc;
} test_audit_proc_index_t;

test_audit_proc_index_t t1[100];

Then

After filling t1.test_mo_proc = fn;
...



in another function
lets say

Sample 1:
void xyz()
{
test_audit_proc_t *proc;
*proc = t1[2].test_mo_proc ; -> Gives Sig 11 here

(*proc)(.......);
}

But if i give
Sample2 :
void xyz()
{
test_audit_proc_t proc;
proc = t1[2].test_mo_proc ; -> Gives Sig 11 here

(proc)(.......);
}
It works !!

But the same code with pointers (ie Sample 1) works for diff set of
function pointers

I just want to know how and why ???
 
D

Dick de Boer

Kathir said:
In C,
Sample 1:
void xyz()
{
test_audit_proc_t *proc;
*proc = t1[2].test_mo_proc ; -> Gives Sig 11 here
proc is a not initialized pointer to function pointer. With *proc you are
addressing whatever proc is pointing to. This causes the Sig 11.

If you are taking care that proc is pointing to a valid location (for a
function pointer) this will work ok.
Note: Type test_audit_proc is already a pointer. No extra stars (*) are
needed.
Sample2 :
void xyz()
{
test_audit_proc_t proc;
proc = t1[2].test_mo_proc ; -> Gives Sig 11 here

Here you are filling the local variable proc with a functionpointer from the
array. OK!

DickB
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,477
Latest member
ColumbusMa

Latest Threads

Top