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?

 
 
Keith Thompson
Guest
Posts: n/a
 
      05-29-2005
Joe Wright <(E-Mail Removed)> writes:
> 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?


The entity it creates is a named alias for an existing type. An
"entity", as I'm using the term, doesn't have to be something that
occupies memory space; it's just something that can be named.

Given:

typedef int foo_t;

"foo_t" is the name of an entity (the alias) that wouldn't exist
without the typedef. Without the typedef, there isn't anything called
"foo_t". (The type int does already exist, but that's not what's
being declared.) By contrast,

extern int foo;

is not a definition because the entity "foo" (which in this case is an
int object) exists with or without the declaration. (If an extern
declaration created a new name, I'd probably argue that it's a
definition that creates an alias for an existing object, but all it
does is refer to an existing object with an existing name.)

--
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
 
 
 
 
S.Tobias
Guest
Posts: n/a
 
      05-29-2005
Joe Wright <(E-Mail Removed)> wrote:
> pete wrote:
> > Keith Thompson wrote:
> >>Joe Wright <(E-Mail Removed)> writes:
> >>>Keith Thompson wrote:


> >>>>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'.


(6.7 Declarations)
# 5 A declaration specifies the interpretation and attributes of a set of
# identifiers. 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;9
# - for an enumeration constant or typedef name, is the (only) declaration
# of the identifier.

I have recently asked in c.s.c (Subject: the term "definition")
the question why the C Std defines the term /definition/ this way,
but I haven't obtained any satisfactory explanation.

[note: C++ is completely different in this respect; notably
a typedef is a declaration, not a definition.]

Interesting thing to note though, is that there seems to be no
clear purpose for the term /definition/ in C language.
I tried to find some usage of the word that would make a difference
whether something is a definition or a (pure) declaration, especially
having typedef in mind, but I found none.
(I'm pretty sure there's none in the Language part, and less sure
about the Library part, where the verb "define" is used (in context
of reserved identifiers), but I think it is used colloquially and
often includes macro definitions; I must yet review that part again.)

[In C++ we have, at least, "One definition rule".]

Perhaps it's better to forget about the nuances created by the Standard
and use the words in ways that everybody understands.

--
Stan Tobias
mailx `echo (E-Mail Removed)LID | sed s/[[:upper:]]//g`
 
Reply With Quote
 
 
 
 
S.Tobias
Guest
Posts: n/a
 
      05-29-2005
Keith Thompson <(E-Mail Removed)> wrote:
> Joe Wright <(E-Mail Removed)> writes:


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


> The entity it creates is a named alias for an existing type. An
> "entity", as I'm using the term, doesn't have to be something that
> occupies memory space; it's just something that can be named.


A typedef declaration just creates another identifier, with
all its consequences.
I'd say a typedef name is even less than an alias, more like
a macro definition:
typedef struct s s_t;
/* s_t is an incomplete type here */
struct s { /*...*/ }; /* creates new type, redeclares `s', implicitly
redeclares (redefines) s_t */
/* s_t is a (different) complete type now */
The meaning of a typedef name depends on the context (therefore we
cannot say that it aliases some _particular_ type).

> Given:


> typedef int foo_t;


> "foo_t" is the name of an entity (the alias) that wouldn't exist
> without the typedef. Without the typedef, there isn't anything called
> "foo_t". (The type int does already exist, but that's not what's
> being declared.) By contrast,


[snip]

But then why other declarations are not definitions?
`foo_t' is just another name for `int' type.
struct s;
creates new type and gives it a name `s' (in tag namespace).
Why doesn't this declaration _define_ the identifier `s'?

(Interestingly, `struct s;' does not "define a new type", but "declares
a new type", 6.7.2.3 p.5 and p.7; this is another mystery what
the difference is.)

--
Stan Tobias
mailx `echo (E-Mail Removed)LID | sed s/[[:upper:]]//g`
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-30-2005
"S.Tobias" <(E-Mail Removed)> writes:
> Joe Wright <(E-Mail Removed)> wrote:

[...]
>> 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'.

>
> (6.7 Declarations)
> # 5 A declaration specifies the interpretation and attributes of a set of
> # identifiers. 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;9
> # - for an enumeration constant or typedef name, is the (only) declaration
> # of the identifier.
>
> I have recently asked in c.s.c (Subject: the term "definition")
> the question why the C Std defines the term /definition/ this way,
> but I haven't obtained any satisfactory explanation.


It seems fairly straightforward to me. A definition creates some
entity and associates a new name with it. For an object definition,
the entity is the object, and creation includes reserving storage for
it. For a function definition, the entity is a function. For a
typedef name, the entity is an alias (or whatever you want to call it)
for a type. For an enumeration constant, it's the enumerator.

In all these cases, the definition *introduces* the identifier and
associates it with the defined entity. Given any of

int foo;
void foo(void) { }
enum { foo };
typedef int foo;

there was nothing called "foo" before the definition. Given:

extern int bar;
void bar(void);

there has to be some pre-existing entity called "bar", whose
definition is elsewhere.

A definition defines (creates) something. A declaration that isn't a
definition merely declares something (i.e., asserts that it already
exists).

--
Keith Thompson (The_Other_Keith) (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
 
Keith Thompson
Guest
Posts: n/a
 
      05-30-2005
"S.Tobias" <(E-Mail Removed)> writes:
> Keith Thompson <(E-Mail Removed)> wrote:
>> Joe Wright <(E-Mail Removed)> writes:

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

>
>> The entity it creates is a named alias for an existing type. An
>> "entity", as I'm using the term, doesn't have to be something that
>> occupies memory space; it's just something that can be named.

>
> A typedef declaration just creates another identifier, with
> all its consequences.


Yes, and anything that creates an identifier is a definition.

> I'd say a typedef name is even less than an alias, more like
> a macro definition:
> typedef struct s s_t;
> /* s_t is an incomplete type here */
> struct s { /*...*/ }; /* creates new type, redeclares `s', implicitly
> redeclares (redefines) s_t */
> /* s_t is a (different) complete type now */
> The meaning of a typedef name depends on the context (therefore we
> cannot say that it aliases some _particular_ type).


It can only alias the type that is the completion of "struct s".

If you're familiar with Unix filesystem semantics, a typedef is
vaguely analogous to a symbolic link. Creating a symbolic link (with
"ln -s") corresponds to a typedef definition -- even if the target of
the symbolic link doesn't exist yet. Creating or changing the target
of the symlink doesn't re-create the symlink itself, it merely creates
or changes the distinct entity to which it refers. (That's probably
not a great analogy.)

The entity created by "typedef struct s s_t;" is a compile-time
*reference* to a type; the definition associates the identifier "s_t"
with that reference. Whether the referenced type is complete or
incomplete may change depending on other declarations, but the
reference exists as soon as the typedef definition itself is
processed.

[...]

> But then why other declarations are not definitions?
> `foo_t' is just another name for `int' type.
> struct s;
> creates new type and gives it a name `s' (in tag namespace).
> Why doesn't this declaration _define_ the identifier `s'?
>
> (Interestingly, `struct s;' does not "define a new type", but "declares
> a new type", 6.7.2.3 p.5 and p.7; this is another mystery what
> the difference is.)


I would say that "struct s;" doesn't define a type, but does define a
tag. I'm not sure whether the standard agrees with me.

--
Keith Thompson (The_Other_Keith) (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
 
baumann@pan
Guest
Posts: n/a
 
      05-30-2005
another question,
how to write a function one of it's parameters is of of function
pointer without typdef?

ie.
func(int a, pfunc a_func_pointer)
{
}
is ok,

but without typedef , there is no pfunc, how can i write the function?

thanks

baumann@pan

 
Reply With Quote
 
ranjeet.gupta@gmail.com
Guest
Posts: n/a
 
      05-30-2005


http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de wrote:
> sathyashrayan <(E-Mail Removed)> wrote:
>
> > "baumann@pan" <(E-Mail Removed)> wrote in message news:(E-Mail Removed) oups.com...
> >> typedef int (*pfunc)(int , int);
> >> pfunc a_func;
> >>
> >> i know it's ok,
> >>
> >> but how can define a_func without typedef statement?

>
> > int (*a_func)(int, int);
> > But this is a declaration not a definition.

>
> No, this creates a variable, named 'a_func', that can hold a pointer
> to a function taking two int arguments and returning an int. No
> 'extern' to see seen in front of it, so it's a definition, not a
> declaration.


Dear Jens !!

what i know is
int *ptr;
this is declaration; ......

same is what is done with the above
int (*a_func)(int, int);
a_fucn is a pointer variable which holds the address of the
function which takes the two argument i.e both the int and
it returns the int.

so it means it is declaration, Now as you suggested if you have
the "extern" if this varaiable is defined outside the file scope,
then i have to define it overe there where I had declared it ??
making it defination ???

Please clarify.
Ranjeet

> Regards, Jens
> --
> \ Jens Thoms Toerring ___ (E-Mail Removed)-berlin.de
> \__________________________ http://www.toerring.de


 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      05-30-2005
(E-Mail Removed) wrote:

> (E-Mail Removed)-berlin.de wrote:
> > sathyashrayan <(E-Mail Removed)> wrote:
> >
> > > int (*a_func)(int, int);
> > > But this is a declaration not a definition.

> >
> > No, this creates a variable, named 'a_func', that can hold a pointer
> > to a function taking two int arguments and returning an int. No
> > 'extern' to see seen in front of it, so it's a definition, not a
> > declaration.

>
> what i know is
> int *ptr;
> this is declaration; ......


Yes, _and_ a definition. It defines, and therefore also declares, a
pointer to int called ptr.

> same is what is done with the above
> int (*a_func)(int, int);
> a_fucn is a pointer variable which holds the address of the
> function which takes the two argument i.e both the int and
> it returns the int.
>
> so it means it is declaration,


_And_ definition.

> Now as you suggested if you have
> the "extern" if this varaiable is defined outside the file scope,
> then i have to define it overe there where I had declared it ??


No. You have do define it somewhere in the program, once. You can then
declare it many times over, anywhere you want to use the pointer.

You want _one_ file in your program where you have an

int *ptr;

This defines the pointer, reserves memory for a single int *, _and_
declares it, so that it can be used in that file from that point.
In every other file where you want to use this pointer, you can then
_declare_ its existence and type using

extern int *ptr;

This will not reserve any memory; it just informs the compiler that
there is a definition for this object, somewhere in the program.

The same thing is true for other objects, and for functions. Define
once, declare as many times as you need it.

Richard
 
Reply With Quote
 
xcm
Guest
Posts: n/a
 
      05-30-2005

> how to write a function one of it's parameters is of of function
> pointer without typdef?
>
> ie.
> func(int a, pfunc a_func_pointer)
> {
> }
> is ok,
>
> but without typedef , there is no pfunc, how can i write the function?
>


#include<stdio.h>
int func(int a, int(*a_func_pointer)(const char*,...) )
{

if (a_func_pointer==printf) a_func_pointer("%s, a\n","hello",a);
}



 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      05-30-2005
Keith Thompson wrote:
> "S.Tobias" <(E-Mail Removed)> writes:
>
>>Keith Thompson <(E-Mail Removed)> wrote:
>>
>>>Joe Wright <(E-Mail Removed)> writes:

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

>>
>>>The entity it creates is a named alias for an existing type. An
>>>"entity", as I'm using the term, doesn't have to be something that
>>>occupies memory space; it's just something that can be named.

>>
>>A typedef declaration just creates another identifier, with
>>all its consequences.

>
>
> Yes, and anything that creates an identifier is a definition.
>
>
>>I'd say a typedef name is even less than an alias, more like
>>a macro definition:
>> typedef struct s s_t;
>> /* s_t is an incomplete type here */
>> struct s { /*...*/ }; /* creates new type, redeclares `s', implicitly
>> redeclares (redefines) s_t */
>> /* s_t is a (different) complete type now */
>>The meaning of a typedef name depends on the context (therefore we
>>cannot say that it aliases some _particular_ type).

>
>
> It can only alias the type that is the completion of "struct s".
>
> If you're familiar with Unix filesystem semantics, a typedef is
> vaguely analogous to a symbolic link. Creating a symbolic link (with
> "ln -s") corresponds to a typedef definition -- even if the target of
> the symbolic link doesn't exist yet. Creating or changing the target
> of the symlink doesn't re-create the symlink itself, it merely creates
> or changes the distinct entity to which it refers. (That's probably
> not a great analogy.)
>
> The entity created by "typedef struct s s_t;" is a compile-time
> *reference* to a type; the definition associates the identifier "s_t"
> with that reference. Whether the referenced type is complete or
> incomplete may change depending on other declarations, but the
> reference exists as soon as the typedef definition itself is
> processed.
>
> [...]
>
>
>>But then why other declarations are not definitions?
>>`foo_t' is just another name for `int' type.
>> struct s;
>>creates new type and gives it a name `s' (in tag namespace).
>>Why doesn't this declaration _define_ the identifier `s'?
>>
>>(Interestingly, `struct s;' does not "define a new type", but "declares
>>a new type", 6.7.2.3 p.5 and p.7; this is another mystery what
>>the difference is.)

>
>
> I would say that "struct s;" doesn't define a type, but does define a
> tag. I'm not sure whether the standard agrees with me.
>


If we make the definition of definition so fuzzy we do not advance the
understanding of others on what we are talking about. Rather than
looking for all sorts of things that might be called definitions, I
choose to define definition itself as creating an object or function so
that I can look at a header (.h) and determine there are no definitions
there, only declarations. Yes Virginia, '#define X 2' is a declaration,
not a definition.

I'm also the quy who maintains that a pointer is an object, not an
address. I maintain that 'char *foo();' has type 'pointer to char' but
returns a value of that type, an address, not a pointer.

char *line;
line = foo();

The object 'line' is the pointer.

I'm not doing this to start another fight. I simply believe we should be
more exclusive than inclusive in the selection of terms used to describe
our precious C programming language. When we tell people "Well, a
pointer could be this, or that. Still it might be something else
depending on context." we are not helping.

--
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