Re: I don't understand typedef example
- From: pete <pfiland@xxxxxxxxxxxxxx>
- Date: Mon, 23 Mar 2009 20:12:24 -0500
André Hänsel wrote:
On 23 Mrz., 03:37, pete <pfil...@xxxxxxxxxxxxxx> wrote:André Hänsel wrote:On 23 Mrz., 01:17, pete <pfil...@xxxxxxxxxxxxxx> wrote:No.André Hänsel wrote:Actually I don't think the author is wrong but he expresses the factsThanks for all your answers. They make it clear. So I understand thatDid you understand the part of the answers
all the author wanted to say is that there is no syntax in C to use a
typedef'd function type to define (or even declare) a function of that
type.
Since the only use of such a typedef is to make a type "pointer to
such a function" I probably will stick to the "typedef int
*pointer_to_such_function(int,int)" syntax in my own code.
which explained about the author being wrong?
You *can* use a typedef to declare a function.
in a very unfortunate way. I think by the sentence "As a word of
warning, typedef can only be used to declare the type of return value
from a function, not the overall type of the function." he means
something like "The typedef'd 'type name' cannot be used to define a
function of this type, it can only be used to define a function
returning a pointer to a funtion of this type."
This is what you meant by "using a typedef to declare a function",
isn't it?
Or is there really a way to define a function of this very type?No.
A typedef can be used to declare a function type.
A typedef can't be used to define a function.
I just posted this code recently:
/* BEGIN new.c */
#include <stdio.h>
#define STRING1 "\n/* BEGIN output new.c */\n"
#define STRING2 "/* END output new.c */"
typedef void *copyfunc(void *s1, const void *s2, size_t n);
/*
** mem_cpy and mem_move
** are functions declared with a copyfunc typedef.
*/
copyfunc mem_cpy;
copyfunc mem_move;
int main(void)
{
char string1[sizeof STRING1];
char string2[sizeof STRING2];
mem_cpy(string1, STRING1, sizeof string1);
mem_move(string2, STRING2, sizeof string2);
puts(string1);
puts(string2);
return 0;
}
void *mem_cpy(void *s1, const void *s2, size_t n)
{
unsigned char *p1 = s1;
const unsigned char *p2 = s2;
while (n-- != 0) {
*p1++ = *p2++;
}
return s1;
}
void *mem_move(void *s1, const void *s2, size_t n)
{
unsigned char *p1 = s1;
const unsigned char *p2 = s2;
p2 += n;
while (p2 != s2 && --p2 != s1) {
;
}
if (p2 != s2) {
p2 = s2;
while (n-- != 0) {
p1[n] = p2[n];
}
} else {
while (n-- != 0) {
*p1++ = *p2++;
}
}
return s1;
}
/* END new.c */
--
pete
Oh, now I understand. Of course I was aware of the difference between
the declaration and the definition, I simply could not understand why
a language may provide a syntax to declare a function using the
typedef but *not* provide a syntax to *define* the function using it.
For any given implementation, all objects of the same type
are the same size and have the same rules for value representation.
The type of a function has nothing
to do with the guts of the function definition.
--
pete
.
- Follow-Ups:
- Re: I don't understand typedef example
- From: Michael Melanson
- Re: I don't understand typedef example
- References:
- I don't understand typedef example
- From: André Hänsel
- Re: I don't understand typedef example
- From: André Hänsel
- Re: I don't understand typedef example
- From: pete
- Re: I don't understand typedef example
- From: André Hänsel
- Re: I don't understand typedef example
- From: pete
- Re: I don't understand typedef example
- From: André Hänsel
- I don't understand typedef example
- Prev by Date: Re: correct macro definitions
- Next by Date: Re: I don't understand typedef example
- Previous by thread: Re: I don't understand typedef example
- Next by thread: Re: I don't understand typedef example
- Index(es):
Relevant Pages
|