Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > how to define a function pointer variable witout typdef?

Reply
Thread Tools

how to define a function pointer variable witout typdef?

 
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      05-28-2005
baumann@pan wrote on 27/05/05 :
> typedef int (*pfunc)(int , int);
> pfunc a_func;
>
> i know it's ok,


Yes, and encouraged to have readable sources...

> but how can define a_func without typedef statement?


You can :

int (*pfunc)(int , int);

by why would you do that except to obscure the code ?

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"

 
Reply With Quote
 
 
 
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      05-28-2005
sathyashrayan wrote on 27/05/05 :
> int (*a_func)(int, int);
> But this is a declaration not a definition.


It actually is a defintion of a non initialized pointer.

extern int (*a_func)(int, int);

would be a declaration

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"

 
Reply With Quote
 
 
 
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      05-28-2005
sathyashrayan wrote on 28/05/05 :
>>> int (*a_func)(int, int);


> Is it the declaration 'a_func' satisfies the criteria of definition
> when 'a_func' pointes with the properly defined function (as you said)? Or I
> am missing some simple thing?


You are making a mixture here.

/* declaration of x */
extern int x;

/* definition of x with an undefined value */
int x;

/* definition of x with a defined value */
int x = 123;

An unitialized variable has a undefined value, but it occupies a region
of memory. The fact that the value is undefined doesn't turn the
variable to be undefined.

Sounds to be a common mistake...

>> No 'extern' to see seen in front of it, so it's a definition, not a
>> declaration.

>
> I dont understand.


That's the point! extern is used to declare a public variable. Read
your C-book again.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

..sig under repair

 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      05-28-2005
Emmanuel Delahaye wrote:
>
> sathyashrayan wrote on 27/05/05 :
> > int (*a_func)(int, int);
> > But this is a declaration not a definition.

>
> It actually is a defintion of a non initialized pointer.
>
> extern int (*a_func)(int, int);
>
> would be a declaration


But the defintion of a non initialized pointer,
is also a declaration.

N869
6.7 Declarations
Semantics
[#5]
A definition of an
identifier is a declaration for that identifier that:
-- for an object, causes storage to be reserved for that
object;
-- for a function, includes the function body;

--
pete
 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      05-28-2005
pete wrote on 28/05/05 :
> Emmanuel Delahaye wrote:
>>
>> sathyashrayan wrote on 27/05/05 :
>>> int (*a_func)(int, int);
>>> But this is a declaration not a definition.

>>
>> It actually is a defintion of a non initialized pointer.
>>
>> extern int (*a_func)(int, int);
>>
>> would be a declaration

>
> But the defintion of a non initialized pointer,
> is also a declaration.
>
> N869
> 6.7 Declarations
> Semantics
> [#5]
> A definition of an
> identifier is a declaration for that identifier that:
> -- for an object, causes storage to be reserved for that
> object;
> -- for a function, includes the function body;


Yes the 'declaration' thing is embedded into the definition.

But a stand-alone declaration certainely is not a defintion.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Mal nommer les choses c'est ajouter du malheur au
monde." -- Albert Camus.

 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      05-28-2005
Keith Thompson wrote:

[ much snippage ]

> It's an object definition (it defines the pointer-to-function object
> "a_func"). It's just not a function definition.
>
> Roughly speaking, a definition is a declaration that creates the
> entity being declared, whereas a declaration that isn't a definition
> merely declares that the entity exists, but doesn't actually create
> it. (All definitions are declarations.) For example:
>
> int x; /* a definition; it creates x */
> extern int y; /* not a definition; y is defined elsewhere */
> void foo(void) { printf("Hello\n"); }
> /* a definition of the function "foo" */
> void bar(void); /* not a definition; bar is defined elsewhere */
>
> Typedefs are a bit odd in that a typedef doesn't actually create a new
> type, merely an alias for an existing type. But a typedef is a
> definition because the thing it creates is the alias, not the type.
>
> And now we wait for the experts to point out my errors.
>


I suppose a typedef is not a definition. This because it does not create
an object in memory. It's another case of C overloading the English
language. Odd, isn't it that '#define X 2' is not a definition either.

--
Joe Wright (E-Mail Removed)
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-28-2005
Joe Wright <(E-Mail Removed)> writes:
> Keith Thompson wrote:

[snip]
>> Typedefs are a bit odd in that a typedef doesn't actually create a
>> new type, merely an alias for an existing type. But a typedef is a
>> definition because the thing it creates is the alias, not the type.
>> And now we wait for the experts to point out my errors.
>>

>
> I suppose a typedef is not a definition. This because it does not
> create an object in memory. It's another case of C overloading the
> English language. Odd, isn't it that '#define X 2' is not a definition
> either.


No, it doesn't create an object in memory. Neither does a function
definition. A definition, as I understand it, is a declaration that
creates the named entity, rather than merely referring to an entity
that's created elsewhere. The entity doesn't have to be an object.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      05-28-2005
Keith Thompson wrote:
>
> Joe Wright <(E-Mail Removed)> writes:
> > Keith Thompson wrote:

> [snip]
> >> Typedefs are a bit odd in that a typedef doesn't actually create a
> >> new type, merely an alias for an existing type. But a typedef is a
> >> definition because the thing it creates is the alias, not the type.
> >> And now we wait for the experts to point out my errors.
> >>

> >
> > I suppose a typedef is not a definition. This because it does not
> > create an object in memory. It's another case of C overloading the
> > English language.
> > Odd, isn't it that '#define X 2' is not a definition
> > either.

>
> No, it doesn't create an object in memory. Neither does a function
> definition. A definition, as I understand it, is a declaration that
> creates the named entity, rather than merely referring to an entity
> that's created elsewhere. The entity doesn't have to be an object.


I only know that typedefs and enums are definitions
because it's been pointed out to me that the standard says so.

External typedefs and enums,
which are both declarations and definitions,
are not definitional enough to be considered as
"external definitions".

--
pete
 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      05-29-2005
Keith Thompson wrote:
> Joe Wright <(E-Mail Removed)> writes:
>
>>Keith Thompson wrote:

>
> [snip]
>
>>>Typedefs are a bit odd in that a typedef doesn't actually create a
>>>new type, merely an alias for an existing type. But a typedef is a
>>>definition because the thing it creates is the alias, not the type.
>>>And now we wait for the experts to point out my errors.
>>>

>>
>>I suppose a typedef is not a definition. This because it does not
>>create an object in memory. It's another case of C overloading the
>>English language. Odd, isn't it that '#define X 2' is not a definition
>>either.

>
>
> No, it doesn't create an object in memory. Neither does a function
> definition. A definition, as I understand it, is a declaration that
> creates the named entity, rather than merely referring to an entity
> that's created elsewhere. The entity doesn't have to be an object.
>


Nice side step. In what way is a typedef a declaration creating a named
entity?

--
Joe Wright (E-Mail Removed)
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      05-29-2005
pete wrote:
> Keith Thompson wrote:
>
>>Joe Wright <(E-Mail Removed)> writes:
>>
>>>Keith Thompson wrote:

>>
>>[snip]
>>
>>>>Typedefs are a bit odd in that a typedef doesn't actually create a
>>>>new type, merely an alias for an existing type. But a typedef is a
>>>>definition because the thing it creates is the alias, not the type.
>>>>And now we wait for the experts to point out my errors.
>>>>
>>>
>>>I suppose a typedef is not a definition. This because it does not
>>>create an object in memory. It's another case of C overloading the
>>>English language.
>>>Odd, isn't it that '#define X 2' is not a definition
>>>either.

>>
>>No, it doesn't create an object in memory. Neither does a function
>>definition. A definition, as I understand it, is a declaration that
>>creates the named entity, rather than merely referring to an entity
>>that's created elsewhere. The entity doesn't have to be an object.

>
>
> I only know that typedefs and enums are definitions
> because it's been pointed out to me that the standard says so.
>
> External typedefs and enums,
> which are both declarations and definitions,
> are not definitional enough to be considered as
> "external definitions".
>


Show me. I have N869 and it makes no reference to typedef reserving
memory for anything. Of course, its very name suggests 'type definition'
in the natural language sense of defining 'last name' as 'surname'.

--
Joe Wright (E-Mail Removed)
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
About typedef -- define the function pointer or define function model? robin liu C Programming 3 04-21-2006 03:26 PM
Windows X64 crashes witout completing boot =?Utf-8?B?RW5yaXF1ZQ==?= Windows 64bit 3 06-19-2005 04:16 PM
How to create an initialise pointer to pointer to function variable; iceColdFire C++ 9 05-23-2005 07:05 AM
can I use __FILE__ ,__LINE__ etc. in a code witout DEBUG defined? baumann.Pan@gmail.com C Programming 5 03-12-2005 12:29 AM



Advertisments