XlC warning "Initialization between types" struct {...} * "and" int "is not allowed. '

The following om.c program is compiled with xlC, except for warning 1506-196!

Anyone who can explain such warning information!

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
    char ch_sh[]="This is a program!!";
    char k1[1200];
    char *ls_sh=NULL;
    size_t len=0;
    FILE *om = open_memstream(&ls_sh, &len);
    fprintf(om, "   %32s", ch_sh);
    if ( argv[1] != NULL )
    {
        fprintf(om, "%26s", argv[1]);
        fclose(om);
        sprintf(k1, "echo %s", ls_sh);
        system(k1);
        printf("\ndone message in program\n");
    }
    else
    {
        printf("Error: Empty input\n");
        return 1;
    }
    free(ls_sh);
}

      

Compiler log:

 1. root@psph:/tmp# 
        root@psph:/tmp# 
        root@psph:/tmp# 
        root@psph:/tmp# xlC -o om om.c
        "om.c", line 11.14: 1506-196 (W) Initialization between types "struct {...}*" and "int" is not allowed.
        root@psph:/tmp# 
        root@psph:/tmp# xlC -qsrcmsg -o om om.c
               11 |     FILE *om = open_memstream(&ls_sh, &len);
                    .............a..............................
        a - 1506-196 (W) Initialization between types "struct {...}*" and "int" is not allowed.

        root@psph:/tmp# ./om data1    
        This is a program!! data1
        done message in program
        root@psph:/tmp# 
        root@psph:/tmp# xlC -qversion
        IBM XL C/C++ for AIX, V11.1 (5724-X13)
        Version: 11.01.0000.0000
        Driver Version: 11.01(C/C++) Level: 100304
        C Front End Version: 11.00(C/C++) Level: 100301
        C++ Front End Version: 11.01(C/C++) Level: 100304
        High-Level Optimizer Version: 11.01(C/C++) and 13.01(Fortran) Level: 100301
        Low-Level Optimizer Version: 11.01(C/C++) and 13.01(Fortran) Level: 100304
        root@psph:/tmp# 
        root@psph:/tmp# 
        root@psph:/tmp#

      

What all!

+3


source to share


4 answers


The warning says you are affecting the int value on a pointer, which is often caused by break level errors. And is open_memstream

not standard C, but POSIX.



Here it is likely that for xlC, it is open_memstream

not declared in stdio.h

. The compiler then assumes that it must be a function that returns an int, hence a warning. You have to ask the person ( man open_memstream

) to find out which include file it is declared in and include it in the source.

+2


source


there are several issues with posted code.

  • the parameter argc

    should always be checked to ensure that the command line argument actually exists. The compiler displays a warning that the parameter is not used

  • the call open_memstream()

    must have a checked return value (! = NULL), and if NULL, then call perror()

    to have the appropriate error message passed to stderr

    , including the text, why the system thinks the call failed.

This way, apart from using the passed parameter argc

and not checking the return value from open_memstream()

, the program compiles and runs without problems.

Note that the warning about argc

can be corrected if main()

the following is stated in the body :



(void)argc;

      

Also, it is bad programming practice to include header files whose content is not used. I.E. remove this statement:

#include <unistd.h>

      

however, the root of YOUR problem is that the xlC compiler has a bug regarding array initialization. This was discussed in:https://stat.ethz.ch/pipermail/r-sig-mac/2003-October/001009.html

0


source


You get a warning:

Initialization between types "struct {...}*" and "int" is not allowed.

      

on this line:

FILE *om = open_memstream(&ls_sh, &len);

      

First problem: open_memstream

not a standard C library function. It is defined by some versions of POSIX. The man page on my Linux system (Ubuntu 16.10) says:

COMPLIANCE WITH POSIX.1-2008. These functions are not specified in POSIX.1-2001 and are not available on other systems.

You may be trying to compile code written for another system (the one that provides open_memstream

on another system (the one that does not open_memstream

). Or it may be available on your system, provided that any spell is needed to make it visible.) Appropriate implementation ISO C is prohibited from defining functions in standard headers other than those defined by the C standard (unless they use reserved names that do not apply here), but there is usually a way to request POSIX compliance, not just ISO C. If that happens, the man page on your system should tell you what you need to do: man open_memstream

You may need to define a macro or pass the -line command to the compiler.

(This could all be easier if all POSIX-specific functions were declared in separate headers rather than being linked to the C-standard line <stdio.h>

, but there are historical reasons why this was not done.)

Second problem: you also asked why you are getting the warning. Prior to the ISO ISO 1999 standard, it was legal to call an undeclared function. The compiler will create an implicit declaration, assuming the function takes any arguments you passed and returns a type result int

. If the actual function doesn't match this assumption, the behavior is undefined. The 1999 standard rejected this "implicit int

" rule , making a call to an undeclared function a constraint violation. But compilers are not required to reject code that violates such a rule; printing a warning and continuing to compile is valid. This means that you must take all the warnings of your C compiler seriously.

(If your system does not open_memstream

, you can search fmemopen

, which appears to be similar. It is also defined by POSIX, not ISO C, so the same considerations apply.)

UPDATE :

Since you are using xlC

, I am assuming you are on an AIX system. Here's the man page for open_memstream

AIX
.

I see there are no instructions for including symbols that are not defined in ISO C. There is an example on the man page. Here's a clipped version that should compile with an error:

#include <stdio.h>
int main(void) {
    char *buf;
    size_t len;
    FILE *f = open_memstream(&buf, &len);
}

      

Try building this example. If you receive a complaint about open_memstream

it, you are most likely using the restrictive command line option (try to compile with only xlc c.c

) or hacked your stdio.h

. If the latter, revert that header file to its original content. If you haven't backed up the original, you really need to stop messing with your system.

Also, if I remember correctly, xlC

is a C ++ compiler; compiler the C xlC

.

0


source


Finally, I need to manually add the following declaration before int main and then it compiles fine.

FILE *open_memstream(char **, size_t *);

      

So Jean-François's solution is right, but why would it only result in such a warning without affecting the final compilation and operation ??

-1


source







All Articles