How to have warning when casting `int_least8_t` to `char`? - c

I am building a string library to support both ascii and utf8.
I create two typedef for t_ascii and t_utf8. ascii is safe to be read as utf8, but utf8 is not safe to be read as ascii.
Do I have any way to issue a warning when implicitely casting from t_utf8 to t_ascii, but not when implicitely casting t_ascii to t_utf8 ?
Ideally, I would want these warnings (and only these warnings) to be issued:
#include <stdint.h>
typedef char t_ascii;
typedef uint_least8_t t_utf8;
int main()
{
t_ascii const* asciistr = "Hello world"; // Ok
t_utf8 const* utf8str = "你好世界"; // Ok
asciistr = utf8str; // Warning: utf8 to ascii is not safe
utf8str = asciistr; // Ok: ascii to utf8 is safe
t_ascii asciichar = 'A';
t_utf8 utf8char = 'B';
asciichar = utf8char; // Warning: utf8 to ascii is not safe
utf8char = asciichar; // Ok: ascii to utf8 is safe
}
Currently, when building with -Wall (and even with -funsigned-char), I get these warnings:
gcc main.c -Wall -Wextra
main.c: In function ‘main’:
main.c:10:35: warning: pointer targets in initialization of ‘const t_utf8 *’ {aka ‘const unsigned char *’} from ‘char *’ differ in signedness [-Wpointer-sign]
10 | t_utf8 const* utf8str = "你好世界"; // Ok
| ^~~~~~~~~~
main.c:12:18: warning: pointer targets in assignment from ‘const t_utf8 *’ {aka ‘const unsigned char *’} to ‘const t_ascii *’ {aka ‘const char *’} differ in signedness [-Wpointer-sign]
12 | asciistr = utf8str; // Warning: utf8 to ascii is not safe
| ^
main.c:16:17: warning: pointer targets in assignment from ‘const t_ascii *’ {aka ‘const char *’} to ‘const t_utf8 *’ {aka ‘const unsigned char *’} differ in signedness [-Wpointer-sign]
16 | utf8str = asciistr; // Ok: ascii to utf8 is safe
| ^

Compile with -Wall. Always compile with -Wall.
<user>#squall:~/src/p1$ gcc -Wall -c test2.c
test2.c: In function ‘main’:
test2.c:9:31: warning: pointer targets in initialization of ‘const t_utf8 *’ {aka ‘const signed char *’} from ‘char *’ differ in signedness [-Wpointer-sign]
9 | t_utf8 const* utf8str = "你好世界";
| ^~~~~~~~~~~~~~
test2.c:11:13: warning: pointer targets in assignment from ‘const t_ascii *’ {aka ‘const char *’} to ‘const t_utf8 *’ {aka ‘const signed char *’} differ in signedness [-Wpointer-sign]
11 | utf8str = asciistr; // Ok: ascii to utf8 is safe
| ^
test2.c:12:14: warning: pointer targets in assignment from ‘const t_utf8 *’ {aka ‘const signed char *’} to ‘const t_ascii *’ {aka ‘const char *’} differ in signedness [-Wpointer-sign]
12 | asciistr = utf8str; // Should issue warning: utf8 to ascii is not safe
| ^
You want it to be safe to cast from t_ascii from t_utf8, but it's simply not. The signedness differs.
The warning is not about the fact that valid utf8 is sometimes not valid ASCII - the compiler knows nothing about that. The warning is about the sign.
If you want an unsigned char, compile with -funsigned-char. But then neither warning will be issued.
(By the way, if you think that type int_least8_t will be able to hold a multibyte char / complete utf8 codepoint encoding - it will not. All int_least8_t and consequently utf8_t in a single compilation unit will have the exact same size.)

Simply compile it with a standard C compiler. What compiler options are recommended for beginners learning C?
Result:
<source>: In function 'main':
<source>:9:31: error: pointer targets in initialization of 'const t_utf8 *' {aka 'const unsigned char *'} from 'char *' differ in signedness [-Wpointer-sign]
9 | t_utf8 const* utf8str = "你好世界"; // Ok
| ^~~~~~~~~~
<source>:11:14: error: pointer targets in assignment from 'const t_utf8 *' {aka 'const unsigned char *'} to 'const t_ascii *' {aka 'const char *'} differ in signedness [-Wpointer-sign]
11 | asciistr = utf8str; // Warning: utf8 to ascii is not safe
| ^
<source>:12:13: error: pointer targets in assignment from 'const t_ascii *' {aka 'const char *'} to 'const t_utf8 *' {aka 'const unsigned char *'} differ in signedness [-Wpointer-sign]
12 | utf8str = asciistr; // Ok: ascii to utf8 is safe
| ^
but not when implicitely casting t_ascii to t_utf8 ?
No you can't have that in standard C, since it's an invalid pointer conversion. You can silence the compiler with an explicit cast, but you are invoking undefined behavior if you do.
Apart from that, you could use C11 _Generic to find out which type uint_least8_t boils down to:
#include <stdint.h>
#include <stdio.h>
#define what_type(obj) printf("%s is same as %s\n", #obj, \
_Generic ((obj), \
char: "char", \
unsigned char: "unsigned char", \
signed char: "signed char") );
int main (void)
{
typedef char t_ascii;
typedef uint_least8_t t_utf8;
t_ascii ascii;
t_utf8 utf8;
what_type(ascii);
what_type(utf8);
}
Output on gcc x86 Linux:
ascii is same as char
utf8 is same as unsigned char

Related

Why do I get incompatible pointer type?

I am trying to use GetVersionEx() from the Windows API with the OSVERSIONINFOEX structure, but this keeps throwing a warning about an incompatible pointer type:
OSVERSIONINFOEX osInfo;
osInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
GetVersionEx(&osInfo);
printf("%ld\n", osInfo.dwBuildNumber);
full warning:
id.c:31:18: warning: passing argument 1 of 'GetVersionExA' from incompatible pointer type [-Wincompatible-pointer-types]
31 | GetVersionEx(&osInfo);
| ^~~~~~~~
| |
| OSVERSIONINFOEX * {aka OSVERSIONINFOEXA *}
In file included from C:/msys64/mingw64/include/winbase.h:36,
from C:/msys64/mingw64/include/windows.h:70,
from id.c:1:
C:/msys64/mingw64/include/sysinfoapi.h:64:61: note: expected 'LPOSVERSIONINFOA' {aka 'struct _OSVERSIONINFOA *'} but argument is of type 'OSVERSIONINFOEX *' {aka 'OSVERSIONINFOEXA *'}
64 | WINBASEAPI WINBOOL WINAPI GetVersionExA (LPOSVERSIONINFOA lpVersionInformation);
| ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
GetVersionEx() expects an OSVERSIONINFO* pointer, not an OSVERSIONINFOEX* pointer. However, the OSVERSIONINFO* pointer can point at an OSVERSIONINFOEX instance, so you just need to use a typecast, eg:
GetVersionEx((LPOSVERSIONINFO)&osInfo);

Pointer pointing to itself

I was exploring typedefs and stumbled upon this program
EDIT: Nearly all the answers were concerned about the warnings it generates. So, I went ahead and removed all the warnings but the question remains the same.
#include<stdio.h>
typedef int int3[3];
int main(){
int a[2][3] = {{1,2,3}, {4,5}};
int3 *p = a;
int *ip = (int *) a;
printf("sizeof:\np: %lu\n(*p+0): %lu\n**p: %lu\nip: %lu\n*ip: %lu\n",sizeof p, sizeof (*p+0), sizeof **p, sizeof ip, sizeof *ip);
printf("---\n");
printf("p: %p\tp+1: %p\n*p: %p\t*p+1: %p\n**p: %d\nip: %p\n*ip: %d",p,p+1,*p,*p+1,**p,ip,*ip);
return 0;
}
on one run it shows:
sizeof:
p: 8
(*p+0): 8
**p: 4
ip: 8
*ip: 4
---
p: 0x7ffe36df31b0 p+1: 0x7ffe36df31bc
*p: 0x7ffe36df31b0 *p+1: 0x7ffe36df31b4
**p: 1
ip: 0x7ffe36df31b0
*ip: 1
My question is:
If p and *p are equal
and **p = 1
then why not *p = 1 as well ?
Provided size of pointer = 8 and size of int = 4
Even if taking in account pointer arithmetic, then *p should at least be 0xkk kk kk kk 00 00 00 01
k being any hex number (consider little-endian)
because de-referencing the same address as int should give 1
EDIT:
To make things clearer, consider this table:
+-----------+----------------------+------------------+
|Variable | Value | Address |
| | | |
| | | |
| p | 0x7ffe36df31b0 | |
| | | |
|*p | 0x7ffe36df31b0 | 0x7ffe36df31b0 |
| | | |
|**p | 1 | 0x7ffe36df31b0 |
+-----------+----------------------+------------------+
How can *p and **p have same address but different value ?
First, note that this code in a confusing mish-mash of constraint violations combined with array decay to generate output of questionable usefulness. But I'll try to answer your question as to what's going on, and try using mundane explanations without resorting to citations from the C Standard, as others already have answered in that manner.
How can *p and **p have same address but different value ?
Because p is a pointer to an array.
Given
typedef int int3[3];
int3 *p;
that means *p is a three-dimensional array. Note that
int3 *p = a;
is a constraint violation, as noted elsewhere. You can force that to "work" with a cast, but it's still fundamentally wrong and the code only "gets away" with it because the address of an array is the address of the first element of that array - and your current implementation doesn't differentiate the pointer types in a significant-enough fashion they they're fundamentally incompatible. Pick an implementation where a plain int * is, say, a 32-bit offset value while an array would have as its address both a 32-bit segment and a 32-bit offset value, and the code would likely generate nonsense if it didn't fail utterly. That's why the C Standard requires pointers to be to "compatible types" - because pointers don't have to be compatible at all.
That problem aside, the initialization/assignment on your implementation can seemingly be treated as
int3 *p = ( int3 * ) a;
Since that "works", that means p contains the address of a three-element array of int values. So dereferencing p with *p is the same as a[0], or the first three-element int array in the two-dimensional a array.
So what is *p, then?
It's exactly the same in this case as a[0], an array. And such a bare array reference decays to an address*, and that address is of the same type as a pointer to an element of the array. And the value of that address will be that of the address of the first element of the array.
So *p, which is a three-element array of int, can itself be dereferenced and evaluate to the first element of a[0], or a[0][0]. So **p is the same as a[0][0].
And a[0][0] - or **p - is an int that is initialized with the value of 1.
While *p is a[0] and the address of that first element.
* - A lot of people say an array "decays to a pointer" or "an array is a pointer", but I like "decays to an address" because a pointer can be assigned to but an address simply is - it can't be assigned to itself, just like an array itself can't be assigned to, but the address can be assigned to something else, like a pointer. Note than when an array is passed to a function, the address of the array actually is passed as an actual pointer. The pointer is the function's parameter and that function parameter can be assigned to...
the posted code, when compiled with:
gcc -c -Wall -Wextra -Wconversion -pedantic -std=gnu11
results in the following compiler messages:
gcc -ggdb -Wall -Wextra -Wconversion -pedantic -std=gnu11 -c "untitled.c" (in directory: /home/richard/Documents/forum)
untitled.c: In function ‘main’:
untitled.c:6:15: warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]
int *ip = a;
^
untitled.c:7:26: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]
printf("sizeof:\np: %d\n(*p+0): %d\n**p: %d\nip: %d\n*ip: %d\n",sizeof p, sizeof (*p+0), sizeof **p, sizeof ip, sizeof *ip);
~^
%ld
untitled.c:7:38: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘long unsigned int’ [-Wformat=]
printf("sizeof:\np: %d\n(*p+0): %d\n**p: %d\nip: %d\n*ip: %d\n",sizeof p, sizeof (*p+0), sizeof **p, sizeof ip, sizeof *ip);
~^
%ld
untitled.c:7:47: warning: format ‘%d’ expects argument of type ‘int’, but argument 4 has type ‘long unsigned int’ [-Wformat=]
printf("sizeof:\np: %d\n(*p+0): %d\n**p: %d\nip: %d\n*ip: %d\n",sizeof p, sizeof (*p+0), sizeof **p, sizeof ip, sizeof *ip);
~^
%ld
untitled.c:7:55: warning: format ‘%d’ expects argument of type ‘int’, but argument 5 has type ‘long unsigned int’ [-Wformat=]
printf("sizeof:\np: %d\n(*p+0): %d\n**p: %d\nip: %d\n*ip: %d\n",sizeof p, sizeof (*p+0), sizeof **p, sizeof ip, sizeof *ip);
~^
%ld
untitled.c:7:64: warning: format ‘%d’ expects argument of type ‘int’, but argument 6 has type ‘long unsigned int’ [-Wformat=]
printf("sizeof:\np: %d\n(*p+0): %d\n**p: %d\nip: %d\n*ip: %d\n",sizeof p, sizeof (*p+0), sizeof **p, sizeof ip, sizeof *ip);
~^
%ld
untitled.c:9:17: warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 2 has type ‘int (*)[3]’ [-Wformat=]
printf("p: %x\tp+1: %x\n*p: %x\t*p+1: %x\n**p: %d\nip: %x\n*ip: %d",p,p+1,*p,*p+1,**p,ip,*ip);
~^
untitled.c:9:26: warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 3 has type ‘int (*)[3]’ [-Wformat=]
printf("p: %x\tp+1: %x\n*p: %x\t*p+1: %x\n**p: %d\nip: %x\n*ip: %d",p,p+1,*p,*p+1,**p,ip,*ip);
~^ ~~~
untitled.c:9:34: warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 4 has type ‘int *’ [-Wformat=]
printf("p: %x\tp+1: %x\n*p: %x\t*p+1: %x\n**p: %d\nip: %x\n*ip: %d",p,p+1,*p,*p+1,**p,ip,*ip);
~^
%ls
untitled.c:9:44: warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 5 has type ‘int *’ [-Wformat=]
printf("p: %x\tp+1: %x\n*p: %x\t*p+1: %x\n**p: %d\nip: %x\n*ip: %d",p,p+1,*p,*p+1,**p,ip,*ip);
~^ ~~~~
%ls
untitled.c:9:61: warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 7 has type ‘int *’ [-Wformat=]
printf("p: %x\tp+1: %x\n*p: %x\t*p+1: %x\n**p: %d\nip: %x\n*ip: %d",p,p+1,*p,*p+1,**p,ip,*ip);
~^
%ls
Compilation finished successfully.
The compiler finishes up by saying the compilation was successful. That does NOT mean that it is ok to run this code and expect valid results/output.
When you correct the code so it cleanly compiles, then please post a EDIT to your question that contains the modified/corrected code.
gcc -ggdb -Wall -Wextra -Wconversion -pedantic -std=gnu11 -c "untitled.c" (in directory: /home/richard/Documents/forum)
untitled.c: In function ‘main’:
untitled.c:6:15: warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]
int *ip = a;
^
untitled.c:7:26: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]
printf("sizeof:\np: %d\n(*p+0): %d\n**p: %d\nip: %d\n*ip: %d\n",sizeof p, sizeof (*p+0), sizeof **p, sizeof ip, sizeof *ip);
~^
%ld
untitled.c:7:38: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘long unsigned int’ [-Wformat=]
printf("sizeof:\np: %d\n(*p+0): %d\n**p: %d\nip: %d\n*ip: %d\n",sizeof p, sizeof (*p+0), sizeof **p, sizeof ip, sizeof *ip);
~^
%ld
untitled.c:7:47: warning: format ‘%d’ expects argument of type ‘int’, but argument 4 has type ‘long unsigned int’ [-Wformat=]
printf("sizeof:\np: %d\n(*p+0): %d\n**p: %d\nip: %d\n*ip: %d\n",sizeof p, sizeof (*p+0), sizeof **p, sizeof ip, sizeof *ip);
~^
%ld
untitled.c:7:55: warning: format ‘%d’ expects argument of type ‘int’, but argument 5 has type ‘long unsigned int’ [-Wformat=]
printf("sizeof:\np: %d\n(*p+0): %d\n**p: %d\nip: %d\n*ip: %d\n",sizeof p, sizeof (*p+0), sizeof **p, sizeof ip, sizeof *ip);
~^
%ld
untitled.c:7:64: warning: format ‘%d’ expects argument of type ‘int’, but argument 6 has type ‘long unsigned int’ [-Wformat=]
printf("sizeof:\np: %d\n(*p+0): %d\n**p: %d\nip: %d\n*ip: %d\n",sizeof p, sizeof (*p+0), sizeof **p, sizeof ip, sizeof *ip);
~^
%ld
untitled.c:9:17: warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 2 has type ‘int (*)[3]’ [-Wformat=]
printf("p: %x\tp+1: %x\n*p: %x\t*p+1: %x\n**p: %d\nip: %x\n*ip: %d",p,p+1,*p,*p+1,**p,ip,*ip);
~^
untitled.c:9:26: warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 3 has type ‘int (*)[3]’ [-Wformat=]
printf("p: %x\tp+1: %x\n*p: %x\t*p+1: %x\n**p: %d\nip: %x\n*ip: %d",p,p+1,*p,*p+1,**p,ip,*ip);
~^ ~~~
untitled.c:9:34: warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 4 has type ‘int *’ [-Wformat=]
printf("p: %x\tp+1: %x\n*p: %x\t*p+1: %x\n**p: %d\nip: %x\n*ip: %d",p,p+1,*p,*p+1,**p,ip,*ip);
~^
%ls
untitled.c:9:44: warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 5 has type ‘int *’ [-Wformat=]
printf("p: %x\tp+1: %x\n*p: %x\t*p+1: %x\n**p: %d\nip: %x\n*ip: %d",p,p+1,*p,*p+1,**p,ip,*ip);
~^ ~~~~
%ls
untitled.c:9:61: warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 7 has type ‘int *’ [-Wformat=]
printf("p: %x\tp+1: %x\n*p: %x\t*p+1: %x\n**p: %d\nip: %x\n*ip: %d",p,p+1,*p,*p+1,**p,ip,*ip);
~^
%ls
Compilation finished successfully.
What you have here is constraint violation because you are using incompatible pointer types in the following initialization:
int *ip = a;
As per C11 standard section on Pointer declarators
For two pointer types to be compatible, both shall be identically qualified and both shall be pointers to compatible types.
and on Simple assignment/Constraints
the left operand has atomic, qualified, or unqualified pointer type, and (considering the type the left operand would have after lvalue conversion) both operands are pointers to qualified or unqualified versions of compatible types, and the type pointed to by the left has all the qualifiers of the type pointed to by the right;
The compiler is require to emit a diagnostic in this case which it does:
<source>:8:15: warning: initialization of 'int *' from incompatible pointer type 'int (*)[3]' [-Wincompatible-pointer-types]
int *ip = a;
See Demo
When you compile a program, it is good practice to enable all warnings and treat warnings as errors so as to avoid (as far as possible) this kind of behavior.

Print out character in c

I'd like to be able to print out the value of a charcter in c as follows:
fprintf("%c", alphabet[val]);
, where alphabet is initialized as
for(i = 0; i < 26; i++){
alphabet[i] = i + 65;
}
However, this line gives the following errors:
encode.c: In function ‘main’:
encode.c:76:13: warning: passing argument 1 of ‘fprintf’ from incompatible pointer type [-Wincompatible-pointer-types]
fprintf("%c", alphabet[val]);
^
In file included from encode.c:1:0:
/usr/include/stdio.h:356:12: note: expected ‘FILE * restrict {aka struct _IO_FILE * restrict}’ but argument is of type ‘char *’
extern int fprintf (FILE *__restrict __stream,
^
encode.c:76:19: warning: passing argument 2 of ‘fprintf’ makes pointer from integer without a cast [-Wint-conversion]
fprintf("%c", alphabet[val]);
^
In file included from encode.c:1:0:
/usr/include/stdio.h:356:12: note: expected ‘const char * restrict’ but argument is of type ‘char’
extern int fprintf (FILE *__restrict __stream,
^
encode.c:76:5: warning: format not a string literal and no format arguments [-Wformat-security]
fprintf("%c", alphabet[val]);
^
How can I print out a character like this?
Check your code closely. For the statement
fprintf("%c", alphabet[val]);
where is the file pointer? It's missing. You have to provide the file pointer, to which you want to see the output, something like
fprintf(fp, "%c", alphabet[val]);
where, fp is the file pointer (of type FILE *), usually returned by fopen(), check the man page for more details on this.
In case, you want the prints to arrive on stdout, i.e., the standard output, use printf().

Is long string literal's type long int*?

According to the answers in this question, a literal like L"test" has type wchar_t[5]. But the following code with GCC seems to say something different:
int main()
{
struct Test{char x;} s;
s="Test"; // ok, char* as expected
s=L"Test"; // ??? I'd expect wchar_t*
return 0;
}
Here's how I compile it (gcc 5.2, but same results are with 4.5 and 4.8):
$ gcc test.c -o test -std=c99
test.c: In function ‘main’:
test.c:4:6: error: incompatible types when assigning to type ‘struct Test’ from type ‘char *’
s="Test"; // ok, char* as expected
^
test.c:5:6: error: incompatible types when assigning to type ‘struct Test’ from type ‘long int *’
s=L"Test"; // ??? I'd expect wchar_t*
^
Apparently, instead of the expected array of wchar_t I get array of long int. What's wrong here?
The type wchar_t is not a fundamental type, like char. It is an implementation-defined synonym of an integer type1.
1 (Quoted from: ISO/IEC 9899:201x 7.19 Common definitions 2.)
wchar_t which is an integer type whose range of values can represent distinct codes for all
members of the largest extended character set specified among the supported locales;

Warnings in compilation

I am trying to compile a simple program written in c on my new desktop. It installed completely normally without any error on my older machine but for some reason it is giving me lots of compilation warnings when I am trying to compile it here. I installed gcc, g++, xpm and xlib which are the pre requisites of this program. Could it be that I am missing some library or something on my new machine?
Here are the warnings I get:
gcc -o view_qsfr qsfr_2005a_tool_box.c -lX11 -lm -lc -L/usr/X11R6/lib -lXpm
In file included from qsfr_2005a_tool_box.c:44:0:
qsfr_2005a_readQSFR.c: In function ‘Read_QSFR’:
qsfr_2005a_readQSFR.c:706:19: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[201]’ [-Wformat=]
scanf("%s",&fname_extra);
^
In file included from qsfr_2005a_graphics.c:8:0,
from qsfr_2005a_tool_box.c:45:
rotated.c: In function ‘XRotPaintAlignedString’:
rotated.c:450:9: warning: return makes integer from pointer without a cast [-Wint-conversion]
return NULL;
^
rotated.c:453:36: warning: passing argument 3 of ‘XCreateGC’ makes integer from pointer without a cast [-Wint-conversion]
my_gc=XCreateGC(dpy, drawable, NULL, 0);
^
In file included from qsfr_2005a_graphics.c:7:0,
from qsfr_2005a_tool_box.c:45:
/usr/include/X11/Xlib.h:1584:11: note: expected ‘long unsigned int’ but argument is of type ‘void *’
extern GC XCreateGC(
^
In file included from qsfr_2005a_graphics.c:8:0,
from qsfr_2005a_tool_box.c:45:
rotated.c:510:45: warning: passing argument 3 of ‘XCreateGC’ makes integer from pointer without a cast [-Wint-conversion]
depth_one_gc=XCreateGC(dpy, empty_stipple, NULL, 0);
^
In file included from qsfr_2005a_graphics.c:7:0,
from qsfr_2005a_tool_box.c:45:
/usr/include/X11/Xlib.h:1584:11: note: expected ‘long unsigned int’ but argument is of type ‘void *’
extern GC XCreateGC(
^
In file included from qsfr_2005a_graphics.c:8:0,
from qsfr_2005a_tool_box.c:45:
rotated.c:566:43: warning: passing argument 3 of ‘XCreateGC’ makes integer from pointer without a cast [-Wint-conversion]
depth_one_gc=XCreateGC(dpy, new_bitmap, NULL, 0);
^
In file included from qsfr_2005a_graphics.c:7:0,
from qsfr_2005a_tool_box.c:45:
/usr/include/X11/Xlib.h:1584:11: note: expected ‘long unsigned int’ but argument is of type ‘void *’
extern GC XCreateGC(
^
In file included from qsfr_2005a_graphics.c:8:0,
from qsfr_2005a_tool_box.c:45:
rotated.c: In function ‘XRotDrawHorizontalString’:
rotated.c:670:36: warning: passing argument 3 of ‘XCreateGC’ makes integer from pointer without a cast [-Wint-conversion]
my_gc=XCreateGC(dpy, drawable, NULL, 0);
^
In file included from qsfr_2005a_graphics.c:7:0,
from qsfr_2005a_tool_box.c:45:
/usr/include/X11/Xlib.h:1584:11: note: expected ‘long unsigned int’ but argument is of type ‘void *’
extern GC XCreateGC(
^
In file included from qsfr_2005a_graphics.c:8:0,
from qsfr_2005a_tool_box.c:45:
rotated.c: In function ‘XRotCreateTextItem’:
rotated.c:982:36: warning: passing argument 3 of ‘XCreateGC’ makes integer from pointer without a cast [-Wint-conversion]
font_gc=XCreateGC(dpy, canvas, NULL, 0);
^
In file included from qsfr_2005a_graphics.c:7:0,
from qsfr_2005a_tool_box.c:45:
/usr/include/X11/Xlib.h:1584:11: note: expected ‘long unsigned int’ but argument is of type ‘void *’
extern GC XCreateGC(
^
In file included from qsfr_2005a_graphics.c:8:0,
from qsfr_2005a_tool_box.c:45:
rotated.c: In function ‘XRotAddToLinkedList’:
rotated.c:1240:18: warning: format ‘%ld’ expects argument of type ‘long int’, but argument 4 has type ‘int’ [-Wformat=]
DEBUG_PRINT4("current cache size=%ld, new item=%ld, limit=%ld\n",
^
rotated.c:66:53: note: in definition of macro ‘DEBUG_PRINT4’
#define DEBUG_PRINT4(a, b, c, d) if (debug) printf (a, b, c, d)
^
rotated.c:1253:15: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long int’ [-Wformat=]
DEBUG_PRINT2("Removed %d bytes\n", i1->size);
^
rotated.c:64:47: note: in definition of macro ‘DEBUG_PRINT2’
#define DEBUG_PRINT2(a, b) if (debug) printf (a, b)
^
In file included from qsfr_2005a_tool_box.c:45:0:
qsfr_2005a_graphics.c: In function ‘initX’:
qsfr_2005a_graphics.c:48:27: warning: passing argument 3 of ‘XCreateGC’ makes integer from pointer without a cast [-Wint-conversion]
gc=XCreateGC(display,win,NULL,NULL);
^
In file included from qsfr_2005a_graphics.c:7:0,
from qsfr_2005a_tool_box.c:45:
/usr/include/X11/Xlib.h:1584:11: note: expected ‘long unsigned int’ but argument is of type ‘void *’
extern GC XCreateGC(
^
In file included from qsfr_2005a_tool_box.c:45:0:
qsfr_2005a_graphics.c: In function ‘Plot_Correlation_Matrix’:
qsfr_2005a_graphics.c:748:10: warning: implicit declaration of function ‘XpmWriteFileFromPixmap’ [-Wimplicit-function-declaration]
XpmWriteFileFromPixmap(display, fname_out, pix, 0, NULL);
^
qsfr_2005a_tool_box.c: At top level:
qsfr_2005a_tool_box.c:413:4: warning: return type defaults to ‘int’ [-Wimplicit-int]
main(int argc, char **argv)
^
physics#XPHYS9G7XGC2LLT:~/Desktop/viewQSFR/viewQSFR$ sudo make
gcc -o view_qsfr qsfr_2005a_tool_box.c -lX11 -lm -lc -L/usr/X11R6/lib -lXpm
In file included from qsfr_2005a_tool_box.c:44:0:
qsfr_2005a_readQSFR.c: In function ‘Read_QSFR’:
qsfr_2005a_readQSFR.c:706:19: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[201]’ [-Wformat=]
scanf("%s",&fname_extra);
^
In file included from qsfr_2005a_graphics.c:8:0,
from qsfr_2005a_tool_box.c:45:
rotated.c: In function ‘XRotPaintAlignedString’:
rotated.c:450:9: warning: return makes integer from pointer without a cast [-Wint-conversion]
return NULL;
^
rotated.c:453:36: warning: passing argument 3 of ‘XCreateGC’ makes integer from pointer without a cast [-Wint-conversion]
my_gc=XCreateGC(dpy, drawable, NULL, 0);
^
In file included from qsfr_2005a_graphics.c:7:0,
from qsfr_2005a_tool_box.c:45:
/usr/include/X11/Xlib.h:1584:11: note: expected ‘long unsigned int’ but argument is of type ‘void *’
extern GC XCreateGC(
^
In file included from qsfr_2005a_graphics.c:8:0,
from qsfr_2005a_tool_box.c:45:
rotated.c:510:45: warning: passing argument 3 of ‘XCreateGC’ makes integer from pointer without a cast [-Wint-conversion]
depth_one_gc=XCreateGC(dpy, empty_stipple, NULL, 0);
^
In file included from qsfr_2005a_graphics.c:7:0,
from qsfr_2005a_tool_box.c:45:
/usr/include/X11/Xlib.h:1584:11: note: expected ‘long unsigned int’ but argument is of type ‘void *’
extern GC XCreateGC(
^
In file included from qsfr_2005a_graphics.c:8:0,
from qsfr_2005a_tool_box.c:45:
rotated.c:566:43: warning: passing argument 3 of ‘XCreateGC’ makes integer from pointer without a cast [-Wint-conversion]
depth_one_gc=XCreateGC(dpy, new_bitmap, NULL, 0);
^
In file included from qsfr_2005a_graphics.c:7:0,
from qsfr_2005a_tool_box.c:45:
/usr/include/X11/Xlib.h:1584:11: note: expected ‘long unsigned int’ but argument is of type ‘void *’
extern GC XCreateGC(
^
In file included from qsfr_2005a_graphics.c:8:0,
from qsfr_2005a_tool_box.c:45:
rotated.c: In function ‘XRotDrawHorizontalString’:
rotated.c:670:36: warning: passing argument 3 of ‘XCreateGC’ makes integer from pointer without a cast [-Wint-conversion]
my_gc=XCreateGC(dpy, drawable, NULL, 0);
^
In file included from qsfr_2005a_graphics.c:7:0,
from qsfr_2005a_tool_box.c:45:
/usr/include/X11/Xlib.h:1584:11: note: expected ‘long unsigned int’ but argument is of type ‘void *’
extern GC XCreateGC(
^
In file included from qsfr_2005a_graphics.c:8:0,
from qsfr_2005a_tool_box.c:45:
rotated.c: In function ‘XRotCreateTextItem’:
rotated.c:982:36: warning: passing argument 3 of ‘XCreateGC’ makes integer from pointer without a cast [-Wint-conversion]
font_gc=XCreateGC(dpy, canvas, NULL, 0);
^
In file included from qsfr_2005a_graphics.c:7:0,
from qsfr_2005a_tool_box.c:45:
/usr/include/X11/Xlib.h:1584:11: note: expected ‘long unsigned int’ but argument is of type ‘void *’
extern GC XCreateGC(
^
In file included from qsfr_2005a_graphics.c:8:0,
from qsfr_2005a_tool_box.c:45:
rotated.c: In function ‘XRotAddToLinkedList’:
rotated.c:1240:18: warning: format ‘%ld’ expects argument of type ‘long int’, but argument 4 has type ‘int’ [-Wformat=]
DEBUG_PRINT4("current cache size=%ld, new item=%ld, limit=%ld\n",
^
rotated.c:66:53: note: in definition of macro ‘DEBUG_PRINT4’
#define DEBUG_PRINT4(a, b, c, d) if (debug) printf (a, b, c, d)
^
rotated.c:1253:15: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long int’ [-Wformat=]
DEBUG_PRINT2("Removed %d bytes\n", i1->size);
^
rotated.c:64:47: note: in definition of macro ‘DEBUG_PRINT2’
#define DEBUG_PRINT2(a, b) if (debug) printf (a, b)
^
In file included from qsfr_2005a_tool_box.c:45:0:
qsfr_2005a_graphics.c: In function ‘initX’:
qsfr_2005a_graphics.c:48:27: warning: passing argument 3 of ‘XCreateGC’ makes integer from pointer without a cast [-Wint-conversion]
gc=XCreateGC(display,win,NULL,NULL);
^
In file included from qsfr_2005a_graphics.c:7:0,
from qsfr_2005a_tool_box.c:45:
/usr/include/X11/Xlib.h:1584:11: note: expected ‘long unsigned int’ but argument is of type ‘void *’
extern GC XCreateGC(
^
In file included from qsfr_2005a_tool_box.c:45:0:
qsfr_2005a_graphics.c: In function ‘Plot_Correlation_Matrix’:
qsfr_2005a_graphics.c:748:10: warning: implicit declaration of function ‘XpmWriteFileFromPixmap’ [-Wimplicit-function-declaration]
XpmWriteFileFromPixmap(display, fname_out, pix, 0, NULL);
^
qsfr_2005a_tool_box.c: At top level:
qsfr_2005a_tool_box.c:413:4: warning: return type defaults to ‘int’ [-Wimplicit-int]
main(int argc, char **argv)
Unless you are using the -Werror flag to turn warnings into errors then the compiler will still have produced an executable that you can run.
The warnings are probably due to the fact that your new system has a newer compiler that is better at detecting problems in your code.
My suggestion would be to fix the warnings if you can/have time. Otherwise just run the program and ignore the warnings (for now, but fix them eventually).
Is your new computer 64 bit?
Is your older computer 32 bit?
The types of warnings I see suggest that you have 32 bit code that you might be trying to compile on a 64 bit computer (as well as the compiler being a much newer version).

Resources