On Jun 8, 2011, at 9:20 PM, Julian Leviston wrote:

> Tanks everyone for answering on this so much...
> 
> Comment/Question below,
> 
> On 09/06/2011, at 4:56 AM, Kevin Jones wrote:
> 
>> I really don't understand what this means:
>> 
>> typedef struct object *(*method_t)(struct object *receiver, ...);
>> 
>> method_t is a pointer to a function that returns an object pointer and takes 
>> receiver and additional argument
> 
> Thanks for this. Okay, I understand that, but why is there a "struct" in 
> there twice? considering object is defined as a struct earlier in the 
> piece... is it because they're object pointers? when specifying a struct 
> pointer, do you need to write "struct" even though you've previously 
> specified a struct with that name?

The latter.  In C++ you only need to use struct when declaring the type.   
However, in C you need to explicitly use struct every time you want to refer to 
the type.

One common idiom is to use a typedef while defining the type.  In this case, 
you might write:

typedef struct object object_t;
typedef object_t *(*method_t)(object_t *receiver, ...);


> 
> ...
> struct vtable;
> struct object;
> struct symbol;
> 
> typedef struct object *(*method_t)(struct object *receiver, ...);
> ...
> 
> This is my reasoning... a function pointer "fp" to a function returning an 
> int and taking an int "h" as an argument is as follows:
> 
> int (*fp)(int h);
> 
> Now, a function pointer "fp2" to a function returning a pointer to an 
> integer, taking an integer pointer "x" as an argument would go like this in 
> my mind:
> 
> int *(*fp2)(int *x);
> 
> Does typedef require that "struct" is included as part of its syntax?

Nope.  In your two examples above, you're declaring variables "fp" and "fp2" 
that can hold function-pointers of the specified type.  Say that you want to 
define a variable "fp3" that can point to a function of the same type as "fp2". 
 You could write:

int *(*fp3)(int *x);

Or you use a typedef before declaring both of them:

typedef int *(*fp_t)(int *x);
fp_t fp2;
fp_t fp3;

As you can see, using typedef doesn't require you to use struct; the only 
reason struct was required is that in C you always need to use it as part of 
the type-name.


> My god, C obfuscates meaning and intention so "well". <sigh>

Yeah, the function-pointer syntax is gnarly.  Hopefully I got it right above ;-)

Cheers,
Josh


> It was one of my first programming languages, yet I still find it incredibly 
> difficult. I guess it's just me coming to the sad realisation that I need to 
> know C (and Math) much better.
> 
> Julian.
> 
> _______________________________________________
> fonc mailing list
> fonc@vpri.org
> http://vpri.org/mailman/listinfo/fonc

_______________________________________________
fonc mailing list
fonc@vpri.org
http://vpri.org/mailman/listinfo/fonc

Reply via email to