what the statement in a structure mean

R

raghu

hello,
Iam going through a document contianing C tricks .In it they
used the statement in many places i didn't understand can u please tell
me the meaning and how the compiler compiles it and runs it
The statement is :
void (* func)( SDS_UINT32 , void * );
which is a member of structure
Thanks in advance
Bye
 
K

kyle.tk

raghu said:
hello,
Iam going through a document contianing C tricks .In it they
used the statement in many places i didn't understand can u please tell
me the meaning and how the compiler compiles it and runs it
The statement is :
void (* func)( SDS_UINT32 , void * );
which is a member of structure
Thanks in advance
Bye

It is a pointer to a function that accepts two arguments, one of type
SDS_UINT32 and the other a void pointer.

-kyle
 
S

sam_cit

raghu said:
hello,
Iam going through a document contianing C tricks .In it they
used the statement in many places i didn't understand can u please tell
me the meaning and how the compiler compiles it and runs it
The statement is :
void (* func)( SDS_UINT32 , void * );
which is a member of structure
Thanks in advance
Bye

Well, func is a pointer to a function taking SDS_UINT32 and void* as
arguements, and which returns void, baisucally it is a function
pointer.

Speaking about function pointers, i have read in many books that it is
helpful in anti-virus software, can anyone tell the exact usage that
makes it so special in anti-virus softwares???
 
R

raghu

kyle.tk said:
It is a pointer to a function that accepts two arguments, one of type
SDS_UINT32 and the other a void pointer.

-kyle

Raghu,
thank u for ur reply
wll it call the function related to it by simply initilization to that
member as similar to it as others did.
 
M

mkaras

raghu said:
Raghu,
thank u for ur reply
wll it call the function related to it by simply initilization to that
member as similar to it as others did.

Initializing the value of a function pointer is a distinctly different
operation than using the function pointer to invoke a subroutine. In
the first case you are setting the value of the function pointer
variable to the address of some code subroutine. In the latter case a
properly initialized function pointer may be used to indirectly call
the subroutine that the is pointed to. They have their usage in program
code where some part of the program may need to call a different
subroutine for each pass through that code.

Now all that said ... function pointers are straightforward ... but can
be very confusing to someone just getting started using them. The
declaration syntax and the invocation syntax can get quite complex
looking when the function pointers specify many arguments and have
return values. On the other hand the function pointer is a very
powerful construct of the C language. If you learn to use them in your
C programs and then find a need to work in another language that does
not have function pointers you can become very frustrated with this
other language.

- mkaras
 
S

sam_cit

On the other hand the function pointer is a very
powerful construct of the C language. If you learn to use them in your
C programs and then find a need to work in another language that does
not have function pointers you can become very frustrated with this
other language.

- mkaras

It would be great if you can you show an example where in a
invocation of a function via pointer makes more sense than a direct
function call?
 
I

Ian Collins

It would be great if you can you show an example where in a
invocation of a function via pointer makes more sense than a direct
function call?
I think you have partly answered your own question, it is common
practice in device drivers to have function pointers to read, write or
control functions as members of a control structure.

The user initialises the function pointers to point to the appropriate
functions in their code and passes the structure to the driver.
 
R

raghu

mkaras said:
Initializing the value of a function pointer is a distinctly different
operation than using the function pointer to invoke a subroutine. In
the first case you are setting the value of the function pointer
variable to the address of some code subroutine. In the latter case a
properly initialized function pointer may be used to indirectly call
the subroutine that the is pointed to. They have their usage in program
code where some part of the program may need to call a different
subroutine for each pass through that code.

Now all that said ... function pointers are straightforward ... but can
be very confusing to someone just getting started using them. The
declaration syntax and the invocation syntax can get quite complex
looking when the function pointers specify many arguments and have
return values. On the other hand the function pointer is a very
powerful construct of the C language. If you learn to use them in your
C programs and then find a need to work in another language that does
not have function pointers you can become very frustrated with this
other language.

- mkaras


Can u please give me an example for this type
Does this statement
c-> func(45,(void *)chara)
 
R

raghu

mkaras said:
Initializing the value of a function pointer is a distinctly different
operation than using the function pointer to invoke a subroutine. In
the first case you are setting the value of the function pointer
variable to the address of some code subroutine. In the latter case a
properly initialized function pointer may be used to indirectly call
the subroutine that the is pointed to. They have their usage in program
code where some part of the program may need to call a different
subroutine for each pass through that code.

Now all that said ... function pointers are straightforward ... but can
be very confusing to someone just getting started using them. The
declaration syntax and the invocation syntax can get quite complex
looking when the function pointers specify many arguments and have
return values. On the other hand the function pointer is a very
powerful construct of the C language. If you learn to use them in your
C programs and then find a need to work in another language that does
not have function pointers you can become very frustrated with this
other language.

- mkaras


Can u please give me an example for this type
what Does this statement mean
c-> func(45,(void *)chara)
 
S

sam_cit

what Does this statement mean
c-> func(45,(void *)chara)

Well, the function pointer which is a member of the structure object
c, is being used to invoke the function it is pointing to.
 
R

Richard Heathfield

(e-mail address removed) said:
Well, the function pointer which is a member of the structure object
c, is being used to invoke the function it is pointing to.

Wrong. c is not a structure object. It's a pointer (or there's a syntax
error in the above).

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
 
T

Tor Rustad

raghu said:
hello,
Iam going through a document contianing C tricks .In it they
used the statement in many places i didn't understand can u please tell
me the meaning and how the compiler compiles it and runs it
The statement is :
void (* func)( SDS_UINT32 , void * );
which is a member of structure

One instructive example is the VM implementation on Linux,
here you can locate the vm_operations_struct:

struct vm_operations_struct
{
void (*open)(struct vm_arena_struct *);
void (*close)(struct vm_arena_struct *);
void (*unmap)(struct vm_arena_struct * ...);
void (*protect)(struct vm_arena_struct * ...);
void (*sync)(struct vm_arena_struct * ...);
....
};

this structure defines the possible function pointers, thus
allowing different operations to be assigned for different
virtual memory areas.

Likewise, operations for file system, communication, device
drivers can be designed in the same way, allowing for dynamic
loading of the allowed operations.
 
S

santosh

Well, the function pointer which is a member of the structure object
c, is being used to invoke the function it is pointing to.

No, c is a pointer. Besides the above will generate a syntax error.
 
R

raghu

Well, the function pointer which is a member of the structure object
c, is being used to invoke the function it is pointing to.
Hello
thanks for ur valuable information i have another doubt,
then how does it will work
Suppose C()is a function with 2 arguments of structure types
can we call D(32,c) where D is another funtion and tell me how it
executes means wheather c is executed first or D or depending on the
situation D will call The C function using the pointer.
Kindly give me the reply
thanks in advance
Bye
 
S

sam_cit

.Hello
thanks for ur valuable information i have another doubt,
then how does it will work
Suppose C()is a function with 2 arguments of structure types
can we call D(32,c) where D is another funtion and tell me how it
executes means wheather c is executed first or D or depending on the
situation D will call The C function using the pointer.
Kindly give me the reply
thanks in advance
Bye

Please post your code...
 
R

raghu

Please post your code...

struct
C


{

void (** M)( struct Mi * );

} ;
C( INT i, void (* M)( struct Mi* ) )

{

int result = SUCCESS;


struct g * gc;

gc->M = M;



return result;

}
this function can be called by
C(32, D);
where D is a function lets say it prints some message like "hello
world".
This is my actual code format .
I am waiting for ur information
Thanks in adv
--Raghu
 
R

Richard Heathfield

raghu said:
Hello
thanks for ur valuable information

What's so valuable about it? It's wrong!

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
 
S

sam_cit

Please post your code...struct
C

{

void (** M)( struct Mi * );

} ; C( INT i, void (* M)( struct Mi* ) )

{

int result = SUCCESS;

struct g * gc;

gc->M = M;

return result;

}this function can be called by
C(32, D);
where D is a function lets say it prints some message like "hello
world".
This is my actual code format .
I am waiting for ur information
Thanks in adv
--Raghu



Does this compile? Anyways, i don't see the code invoking the
function pointed to by the pointers...
 
C

CBFalconer

raghu said:
Please post your code...

struct
C
{

void (** M)( struct Mi * );
} ;
C( INT i, void (* M)( struct Mi* ) )
{
int result = SUCCESS;
struct g * gc;

gc->M = M;
return result;
}


Post compilable code. The above is nonsense.
 

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
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top