Thread: Problem uder LINUX (gcc) and same code work at Windows (DevC++)

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    6

    Problem uder LINUX (gcc) and same code work at Windows (DevC++)

    I have this code:

    Code:
    #include <stdio.h>
    
    main()
        {
    
        int a,b,c;
        int *pa,*pb,*pc;
    
        a = 10;
        pa = &a;
    
        printf("Value of a=%d, b=%d, c=%d\n",a,b,c);
        printf("Address of a, b, c are %d %d %d\n",&a,&b,&c);
    
        pb = &b;
        *pb = *pa;
    
        printf("Values of a=%d, b=%d, c=%d\n",a,b,c);
        printf("Address of a, b, c are %d %d %d\n",&a,&b,&c);
    
        *(--pb) = *pb;
    
        printf("Values of a=%d, b=%d, c=%d\n",a,b,c);
        printf("Address of a, b, c areu %d %d %d\n",&a,&b,&c);
    
        }
    This above code on Widows (DevC++) work, with only one code more on end [system("PAUSE")] to stop program before exit.

    If I tray to compile this code at Linux (gcc -o address address.c) I get list of errors.

    Code:
    adresa.c: In function ‘main’:
    adresa.c:13: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
    adresa.c:13: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘int *’
    adresa.c:13: warning: format ‘%d’ expects type ‘int’, but argument 4 has type ‘int *’
    adresa.c:19: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
    adresa.c:19: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘int *’
    adresa.c:19: warning: format ‘%d’ expects type ‘int’, but argument 4 has type ‘int *’
    adresa.c:24: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
    adresa.c:24: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘int *’
    adresa.c:24: warning: format ‘%d’ expects type ‘int’, but argument 4 has type ‘int *’
    Why is this happening and how to resolve this.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Take a look at the formatting strings for printf() ... I don't think you want %d to print an address.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    6
    ADD to orginal post

    I have gcc --version

    gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3 ....

    I tray to use %f, and %lo but errors are not resolved
    What to use insted %d

    sizeof(int) = 4
    If I'm on right this is mean 2^31 - that number I can put in INT

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    6
    If I use %p instead of %d I get my address

    but

    How this work on Windows with %d and on Linux no accept %d

    my address have format: 0xbfe2ea54
    this is hexadecimal

    can I use than %x or %X
    or can I transform hexadecimal address to decimal or binary type.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by miroslavgojic View Post
    If I use %p instead of %d I get my address

    but

    How this work on Windows with %d and on Linux no accept %d

    my address have format: 0xbfe2ea54
    this is hexadecimal

    can I use than %x or %X
    or can I transform hexadecimal address to decimal or binary type.
    Honestly, I have no real idea how that would work on windows but not linux.
    unless there is some difference between the libraries...

    Yes you can use %x or %X which will display addresses as hexidecimal numbers, which is the traditional format for pointers.

    Finally... you should try to work with a single cross-platform compiler. Dev C++ is a deadh horse, that is no longer supported. There are several free compilers you can choose from but at minimum you want C-99 compliance on both platforms.
    Last edited by CommonTater; 11-24-2010 at 09:25 AM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The gcc that comes with Dev-C++ is a lot older than the one you get with an up to date Linux.

    Consequently, it checks less things by default.

    GCC online documentation - GNU Project - Free Software Foundation (FSF)
    Find which version relates to your dev-cpp, and see if it supports something like -Wformat
    Code:
    `-Wformat'
         Check calls to `printf' and `scanf', etc., to make sure that the
         arguments supplied have types appropriate to the format string
         specified, and that the conversions specified in the format string
         make sense.  This includes standard functions, and others
         specified by format attributes (*note Function Attributes::), in
         the `printf', `scanf', `strftime' and `strfmon' (an X/Open
         extension, not in the C standard) families (or other
         target-specific families).  Which functions are checked without
         format attributes having been specified depends on the standard
         version selected, and such checks of functions without the
         attribute specified are disabled by `-ffreestanding' or
         `-fno-builtin'.
    If the flag is present, then you should be able to use it yourself.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Tags for this Thread