News:

Download Pelles C here: http://www.smorgasbordet.com/pellesc/

Main Menu

pragma warn

Started by czerny, December 09, 2012, 02:26:17 PM

Previous topic - Next topic

czerny

Hello,

I have a header file:

...
#pragma warn (disable : 2145)
typedef int (*DataFunc)(void *);
...

which defines a generic function pointer 'DataFunc' which itself needs a void pointer as parameter.

This function pointer is later realized for example by a function:

int worker(int *p)
{
printf("%d\n", *p);
return 1;
}

Without the above #pragma, the compiler gives a warning #2145: Assignment of 'int __cdecl function(int *)' to 'int __cdecl function(void *)'.
With the above #pragma the compiler disables the warning globally, which I do not want.
Is there a way to disable the warning 2145 just for this single function pointer definition?

I have seen the #pragma warn (push|pop) but could not get what I am looking for.

czerny
Fertig.

laurro

With :


...
#pragma warn (disable : 2145)
typedef int (*DataFunc)(void *);
#pragma warn (enable: 2145)
...


Laur.

aardvajk

Quote
Is there a way to disable the warning 2145 just for this single function pointer definition?
I don't think so, and you wouldn't want to anyway since casting function pointers and using them is undefined behaviour.

What is defined (for data pointers) is casting to void* and back to the original type. So, a solution that's guaranteed to work and shouldn't cause any warnings is to change worker's definition to
int worker(void*)
and cast the argument back to int* inside the function.

czerny

Quote from: laurro on December 09, 2012, 03:04:26 PM
With :


...
#pragma warn (disable : 2145)
typedef int (*DataFunc)(void *);
#pragma warn (enable: 2145)
...


Laur.

No!

See the following example:

#pragma warn (disable : 2145)
typedef int  (*DataFunc1)(void *);
#pragma warn (enable : 2145)
typedef int  (*DataFunc2)(void *, void *);

int Func1(int *p){}
int Func2(int *p1, int *p2){}

int main(int argc, char **argv)
{
DataFunc1 f1 = Func1;
DataFunc2 f2 = Func2;
return 1;
}


czerny

czerny

Quote from: aardvajk on December 09, 2012, 09:50:32 PM
I don't think so, and you wouldn't want to anyway since casting function pointers and using them is undefined behaviour.
I do not want to cast a function pointer.

czerny

laurro

try this way:

#include <stdio.h>

typedef int  (*DataFunc1)(void *);
typedef int  (*DataFunc2)(void *, void *);

  int Func1(int *p){return *p*2;}
  int Func2(int *p1, int *p2){return *p1+*p2;}

#pragma warn (disable : 2145 )
DataFunc1 f1 = Func1;

#pragma warn (enable : 2145 )
DataFunc2 f2 = Func2;


int main(int argc, char **argv)
{
int a = 10;
int b = f1(&a);
printf("%d\n\n", b);

return f2(&a,&b);
}


Laur

czerny

Quote from: laurro on December 10, 2012, 10:49:05 AM
try this way:
Sorry! This solves the problem at the wrong place!

In reallity I have to solve this in a multi source file situation. The function pointers are declared in a separate module (*.c,*h) and the user (the writer of the main program per example) should not set any #pragmas which are  caused by that module.

jj2007

Hi,
I am using
#pragma warn(disable:2007)        // assembly not portable
#pragma warn(disable:2215)        // conversion ... loss of data
#pragma warn(disable:2216)        // retval never used
#pragma warn(disable:2218)        // para not referenced

Works fine except for the 2218 which gets happily ignored by PellesC. Is that a known behaviour?

CLR


jj2007

Yes - stupid typo!! Thanxalot, it was driving me mad :)