Re: I don't understand typedef example



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:
André Hänsel wrote:
Thanks for all your answers. They make it clear. So I understand that
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.
Did you understand the part of the answers
which explained about the author being wrong?
You *can* use a typedef to declare a function.
Actually I don't think the author is wrong but he expresses the facts
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?
No.

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
.



Relevant Pages

  • Re: I dont understand typedef example
    ... Since the only use of such a typedef is to make a type "pointer to ... You *can* use a typedef to declare a function. ... char string1[sizeof STRING1]; ... const unsigned char *p2 = s2; ...
    (comp.lang.c)
  • Re: If you could change the C or C++ or Java syntax, what would you like different?
    ... in the way it was done (i.e., refer to the declaration of a new type as "strong typedef"), you ... when you declare a new type the main motivation is to benefit from the need to comply with the ... There would be little desire to create a pointer type which is ... we want implicit conversion rules between ...
    (comp.lang.c)
  • Re: I dont understand typedef example
    ... * Using typedef, declare 'func' to have type ... 'function taking two int arguments, ... declared ptr as a pointer object that can points to a function of the ...
    (comp.lang.c)
  • Re: I dont understand typedef example
    ... Using typedef, declare 'func' to have type ... 'function taking two int arguments, ...
    (comp.lang.c)
  • Re: Simple Declaration Question
    ... > You can declare several variables in one statement. ... you must include the data type for each variable. ... > Dim intX As Integer, intY As Integer, intZ As Integer ... > Dim String1, String2 as String ...
    (microsoft.public.excel.programming)