Struct containing pointer to function 
Author Message
 Struct containing pointer to function

Quote:

> int main(void)
> {
> struct test_fptr *p_stru = NULL;
> int ret_val;

> p_stru->func_point = func;

You didn't point p_stru to anything, but then you attempt to
access one of its members.  BANG!
--
"I should killfile you where you stand, worthless human." --Kaz


Mon, 03 Mar 2003 03:00:00 GMT  
 Struct containing pointer to function

: My goal is to be able to have a struct containing a pointer to a function,
: (*func_point)(), which I can initialise to some function, func(), and make
: calls to that function via the pointer/structure.

: I am using Borland Turbo C, v3.0 with the source options set to ANSI C and
: the whole sha-bang is running in a DOS window on win95.

: As the function stands (see below), it compiles just fine, but when I run
: it I get the following output:

: Func called, k = 4
: ret_val = 4
: Null pointer assignment

: I understand (via the compiler's online help) that the "Null pointer
: assignment" is due to some value being stored to an uninitialised pointer
: but it isn't obvious to me where this is.

: I would appreciate anyone's help with this.

: Thank you...

: David C

: /** Start program *************************************/

: #include <stdio.h>

: /* Function prototype */
: int func(int k);

: struct test_fptr
:   {
:   int (*func_point)(int);
:   };

: int main(void)
: {
: struct test_fptr *p_stru = NULL;
: int ret_val;

: p_stru->func_point = func;

Here's your problem. Your pointer p_stru points to NULL, yet you try to
access its fields. This causes undefined behaviour.
To correct this:
Put #include <stdlib.h> at the top of the file
Insert here:
p_stru=malloc(sizeof(struct test_fptr));
if (!p_stru)
{
  printf("Sorry user, I don't have enough memory available\n");
  return 0;

Quote:
}

: ret_val = p_stru->func_point(4);

: printf("ret_val = %d\n", ret_val);

And insert here:
free(p_stru);

: return 0;
: }

: int func(int k)
: {
: printf("Func called, k = %d\n", k);

: return k;
: }

: /** End program ***************************************/

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #80 D+ ADA N+++ |
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/

"Ice cream sales somehow cause drownings: both happen in summer."
   - Antti Voipio & Arto Wikla



Mon, 03 Mar 2003 03:00:00 GMT  
 Struct containing pointer to function
Hi David,

your structure definition was right, but you local variable was wrong. You
defined a pointer to your struct with your pointer which was not
initialized!

So you should define a variable of your struct type or if you really want a
pointer you have to do a malloc():

#include <stdio.h>
#include <malloc.h>

/* Function prototype */
int func(int k);

struct test_fptr
  {
  int (*func_point)(int);
  };

int main(void)
{
  struct test_fptr *p_stru = NULL;
  struct test_fptr stru;
  int ret_val;

  p_stru = (struct test_fptr *)
    malloc( sizeof(test_fptr) );
  p_stru->func_point = func;
  ret_val = p_stru->func_point(4);
  printf("ret_val = %d\n", ret_val);
  free(p_stru);

  stru.func_point = func;
  ret_val = stru.func_point(5);
  printf("ret_val = %d\n", ret_val);

  return 0;

Quote:
}

int func(int k)
{
  printf("Func called, k = %d\n", k);

  return k;

Quote:
}



Mon, 03 Mar 2003 03:00:00 GMT  
 Struct containing pointer to function
My goal is to be able to have a struct containing a pointer to a function,
(*func_point)(), which I can initialise to some function, func(), and make
calls to that function via the pointer/structure.

I am using Borland Turbo C, v3.0 with the source options set to ANSI C and
the whole sha-bang is running in a DOS window on win95.

As the function stands (see below), it compiles just fine, but when I run
it I get the following output:

Func called, k = 4
ret_val = 4
Null pointer assignment

I understand (via the compiler's online help) that the "Null pointer
assignment" is due to some value being stored to an uninitialised pointer
but it isn't obvious to me where this is.

I would appreciate anyone's help with this.

Thank you...

David C

/** Start program *************************************/

#include <stdio.h>

/* Function prototype */
int func(int k);

struct test_fptr
  {
  int (*func_point)(int);
  };

int main(void)
{
struct test_fptr *p_stru = NULL;
int ret_val;

p_stru->func_point = func;

ret_val = p_stru->func_point(4);

printf("ret_val = %d\n", ret_val);

return 0;

Quote:
}

int func(int k)
{
printf("Func called, k = %d\n", k);

return k;

Quote:
}

/** End program ***************************************/


Mon, 03 Mar 2003 22:01:58 GMT  
 Struct containing pointer to function

Quote:

>As the function stands (see below), it compiles just fine, but when I run
>it I get the following output:

>Func called, k = 4
>ret_val = 4
>Null pointer assignment

>I understand (via the compiler's online help) that the "Null pointer
>assignment" is due to some value being stored to an uninitialised pointer
>but it isn't obvious to me where this is.

That's exactly what you have--an uninitialized pointer.  See comments
below.

Quote:
>/** Start program *************************************/

>#include <stdio.h>

>/* Function prototype */
>int func(int k);

>struct test_fptr
>  {
>  int (*func_point)(int);
>  };

>int main(void)
>{
>struct test_fptr *p_stru = NULL;

Here, you create a pointer to a test_fptr structure and assign it to
NULL.  This is harmless, but meaningless in context.

Quote:
>int ret_val;

>p_stru->func_point = func;

Ask yourself this:  Where does p_stru point to?

You tried to access the member of a struct using a NULL pointer.  Watch
those nose demons fly!

Pointers are well and good, but eventually (preferably before use) they
have to point *somewhere*.  Make that, "somewhere *useful*."

Try this:

        int main(void)
        {
                struct test_fptr stru;
                int ret_val;

                stru.func_point = func;
                ret_val = stru.func_point(4);

                printf("ret_val = %d\n", ret_val);
                return 0;

Quote:
}

If you want to continue using a pointer to access the struct, try this:

        int main(void)
        {
                struct test_fptr stru, *p;
                int ret_val;

                p = &stru;
                p->func_point = func;
                ret_val = p->func_point(4);

                printf("ret_val = %d\n", ret_val);
                return 0;

Quote:
}

--
Robert B. Clark
Visit ClarkWehyr Enterprises On-Line at http://home.earthlink.net/~rclark31/ClarkWehyr.html


Tue, 04 Mar 2003 08:08:46 GMT  
 Struct containing pointer to function


Quote:
> My goal is to be able to have a struct containing a pointer to a
function,
> (*func_point)(), which I can initialise to some function, func(), and
make
> calls to that function via the pointer/structure.

> I am using Borland Turbo C, v3.0 with the source options set to ANSI C
and
> the whole sha-bang is running in a DOS window on win95.

> As the function stands (see below), it compiles just fine, but when I
run
> it I get the following output:

> Func called, k = 4
> ret_val = 4
> Null pointer assignment

> I understand (via the compiler's online help) that the "Null pointer
> assignment" is due to some value being stored to an uninitialised
pointer
> but it isn't obvious to me where this is.

> /** Start program *************************************/

> #include <stdio.h>

> /* Function prototype */
> int func(int k);

> struct test_fptr
>   {
>   int (*func_point)(int);
>   };

> int main(void)
> {
> struct test_fptr *p_stru = NULL;
> int ret_val;

> p_stru->func_point = func;

as a side issue, is func() in scope here? In my programming style I
prefer to have encountered something textually before I take it's
address.

- Show quoted text -

Quote:

> ret_val = p_stru->func_point(4);

> printf("ret_val = %d\n", ret_val);

> return 0;
> }

> int func(int k)
> {
> printf("Func called, k = %d\n", k);

> return k;
> }

> /** End program ***************************************/

--
Software, regardless of the language or OS, is being used to handle
real-world, life-or-death problems. THAT should cause fear, except
that the alternative is for every single emergency to be handled
entirely by humans...

Sent via Deja.com http://www.deja.com/
Before you buy.



Tue, 04 Mar 2003 03:00:00 GMT  
 Struct containing pointer to function

Quote:



> > /** Start program *************************************/

> > #include <stdio.h>

> > /* Function prototype */
> > int func(int k);

> > struct test_fptr
> >   {
> >   int (*func_point)(int);
> >   };

> > int main(void)
> > {
> > struct test_fptr *p_stru = NULL;
> > int ret_val;

> > p_stru->func_point = func;

> as a side issue, is func() in scope here? In my programming style I
> prefer to have encountered something textually before I take it's
> address.

Your apparent understanding of "scope" mystifies me.  The name "func" is
in scope because it has earlier been declared in the line commented as
"Function prototype", at the compilation unit level.

Your reference to "textual ... encounter" is not familiar to me.  If a
name is in scope, it may be used.  Usage includes taking the address
of the object so named.  If a name is not in scope, it cannot be used,
and the compiler must report an error.

It is not a matter of preference, but of language definition.
--
Jonathan Headland
WindRiver
Jakob-Haringer-Str. 8
A-5020 Salzburg, Austria



Tue, 04 Mar 2003 03:00:00 GMT  
 Struct containing pointer to function


Quote:



> > > /** Start program *************************************/

> > > #include <stdio.h>

> > > /* Function prototype */
> > > int func(int k);

> > > struct test_fptr
> > >   {
> > >   int (*func_point)(int);
> > >   };

> > > int main(void)
> > > {
> > > struct test_fptr *p_stru = NULL;
> > > int ret_val;

> > > p_stru->func_point = func;

> > as a side issue, is func() in scope here? In my programming style I
> > prefer to have encountered something textually before I take it's
> > address.

> Your apparent understanding of "scope" mystifies me.  The name "func"
> is in scope because it has earlier been declared in the line commented
> as "Function prototype", at the compilation unit level.

> Your reference to "textual ... encounter" is not familiar to me.  If a
> name is in scope, it may be used.  Usage includes taking the address
> of the object so named.  If a name is not in scope, it cannot be used,
> and the compiler must report an error.

> It is not a matter of preference, but of language definition.

I knew I'd made a mistake as soon as I pressed the "send" button. But
I thought I'd give someone the small pleasure of shooting me down  :-).

My use of the word "scope" was sloppy in the extreme. I very rarely
take the address of a function that appears below. This is because I
write my programs "backwards" due to years of Pascal. main() is the
last function in the file it appears in.

--
Abuse of casting leads to abuse of the type system
leads to sloppy programming
leads to unreliably, even undefined, behaviour.
And that is the path to the dark side....
                         Richard Bos/John Hascall

Sent via Deja.com http://www.deja.com/
Before you buy.



Tue, 04 Mar 2003 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. pointers to functions in structs which take a pointer to that struct as an arg

2. Pointers to Structure that contains pointer to Function

3. free()'ing structs containing pointers to memory?

4. Malloc & structs contain struct*

5. Initialisation of structs containing arrays of structs...?

6. native dll calling with structs containing arrays of nested structs

7. memory block containing pointers, aka pointer to pointer i believe

8. Dispatch tables with functions containing managed pointers

9. cast pointer to struct to pointer to type of first member of struct

10. Pointer to a function containing variable argument list

11. How to use (pointer to function), and function and pointer to (pointer to function)

12. Arrays containing pointers to functions...

 

 
Powered by phpBB® Forum Software