Use the TYPEDEF!
Use the TYPEDEF!
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Did you ever get the declaration of your array fixed?
So what does this really mean? You are trying to create a 2D array and also an array of pointers to functions that return an integer. You can do either one or the other but not both.
Code:int status = (*FIPFunc)(arg1, arg2); /* FIPFunc is a pointer to a function that takes 2 arguments and returns an int */ int (*FIPFunc[10]) (arg1, arg2); /* this is an array of pointers to functions that take 2 arguments and return an int */
Sorry bout that didn't understand until i read more on it but i tried
And theres no such luck im not sure how to typedef it since its 2-DCode:typedef void(*FIPF)(Uint8,Uint32); int status = (FIPFunc[(Uint32) hdr->Code] [(Uint32) hdr->Subcode])(DescListPtr, DescListLen);
And I did get declaring it to work since i used a
void* FIPFunc.......
and the void* allowed it to compile ......
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
I tried this out but i get this warningCode:typedef (*FIPF)(int); FIPF FIPFunc[4][2]; void FIPinit(){ FIPFunc[0][0] = &FIPDiscSolicit; } int FIPDiscSolicit(Uint8 *DescListPtr, Uint32 DescListLen) { }
fip.c:19: warning: assignment from incompatible pointer type
The warning is in the FIPInit() but iono whats wrong wit the pointer type
O yea and I'm having a problem calling it now
int status = (FIPFunc[(Uint32) hdr->Code] [(Uint32) hdr->Subcode])(DescListPtr, DescListLen);
I get
too many arguments to function ‘FIPFunc[(unsigned int)hdr->Code][(unsigned int)hdr->Subcode]’
UHH YES IT COMPILED iono if really works but at least it compiled im going to test it around now see if its all okay
Last edited by kiros88; 09-15-2009 at 03:33 PM. Reason: IT COMPILED
Why are you using the address-of operator?
Quzah.
Hope is the first step on the road to disappointment.
So why not do the typedef to match what it's supposed to be?
(Edit: And if you haven't prototyped the function, the compiler won't know that it's a function either....)Code:typedef int (*FIPF)(Uint8 *, Uint32);
Out of curiosity, why do you want a 2D array of function pointers? What are you trying to do here?
Ditto. kiros88, I gave you an example: do you see & there? You have to pay attention to details or they will just bury you.
Also, the prototypes need to match, look:
BTW, for a non int function you need to include the type:Code:typedef (*FIPF)(int); /* this function has one arg, an int */ int FIPDiscSolicit(Uint8 *DescListPtr, Uint32 DescListLen) /* this function has two args, niether of them int */
returns char *ptr.Code:typedef char *(*FPtr)(int);
Last edited by MK27; 09-15-2009 at 03:56 PM.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
I see no reason why you would ever leave the type off (especially since implicit int disappeared in C99).BTW, for a non int function you need to include the type:
Also, as a more general question, what's the problem with using the address-of operator on a function? I don't do it myself, but there's nothing wrong with it. It makes some sense to use it, I think, to make it obvious what the code is doing. It's somewhat akin to using &array[0] instead of array; both work just fine and mean the same thing (again, in most contexts). Unless I'm missing something obvious here, of course.
Yeah,
a and &a[0] are the same, but are not the same with &a, which I believe was the error here
C: A Reference Manual, 5th ed, page 208:
The name of a function evaluates to that function; it is not an lvalue. Unless the function name is the argument of the address operator (&) or the argument to sizeof, the name is converted to a pointer to the function as part of the usual unary conversions. The result of &f is a pointer to f, not a pointer to a pointer to f, and sizeof(f) is invalid.
[edit]
In case anyone cares...
C: A Reference Manual, 5th ed, page 167:
An expression of type "function returning..." that is not used in a function call, as the argument of the address operator, &, or as the argument of the sizeob operator is immediately converted to the type "pointer to function returning...." (Not performing the conversion when the function is the argument of sizeof ensures that the sizeof expression will be invalid and not just result in the size of a pointer.) The ony expressions that can yield a value type of "function returning T" are the name of such a function and an indirection expression consisting of the unary indirection operator, *, applied to an expression of type "pointer to function returning...."
Example
The following program assigns the same pointer value to fp1 and fp2:
extern int f();
int (*fp1)(), (*fp2)();
fp1 = f; /* implicit conversion to pointer */
fp2 = &f; /* explicit manufacture of a pointer */
[/edit]
Quzah.
Last edited by quzah; 09-15-2009 at 07:37 PM.
Hope is the first step on the road to disappointment.
Right. When I say corner cases I mean you can't apply & to &func (which is pretty obvious), and you can't apply sizeof to func (which is less obvious). In other cases, it's treated as a pointer to the function. So when you're passing a function (pointer) to another function, or assigning it to a function pointer variable, and so on, you can either use the & or not. Both are valid.