Thread: Why 1 of them is working and other one doesn't??

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    225

    Why 1 of them is working and other one doesn't??

    hello,
    the code is posted below.Why 1 is working and other one doesn't even though both point to the base address of 2D array


    Code:
    #include <stdio.h>
    void display(int a[][4])
    {
      a[0][0]=1;
    }
    
    
    int main(void)
    {
     int a[3][4];
     display(a); //this works
     display(&a[0][0]); //this doesn't work
     getch();
     return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Even though it has the same value, &a[0][0] is the wrong type.

    Another example
    Code:
    struct foo {
      int bar;
    };
    
    void func ( struct foo *p ) {
    }
    
    int main ( ) {
      struct foo a;
      func( &a ); // good
      func( &a.bar );  // bad
    }
    Same value, wrong type.
    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.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Ok and why this one works even though it shouldn't .It just produces warnings but it gives the correct output!
    Code:
    #include <stdio.h>
    void display(char **a)
    {
     printf(a);
    }
    
    
    int main(void)
    {
      char s[]="Hello World";
      display(s);
      return 0;
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Why what?

    Why it works?
    Why your compiler lets you?
    Why you feel the need to experiment with rubbish code to see what happens?

    I mean, it's only slightly more sloppy to write
    void display ( int a )
    and if your compiler accepts it, it would still work.

    Since all you care about is the 'value' of a, which starts of correct, then all you have is (with warnings) is
    void display ( irrelevant_type a )
    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.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Because you are just breaking the promises between the function definition and the function use - you are passing the correct value to printf, but the wrong type. In most architectures, all pointers are essentially the same, so if we mess about with different types of pointers, but pass the right thing, then it will still work.

    For example:
    Code:
    void func(float *pf)
    {
       printf("%s", pf);
    }
    
    int main()
    {
        char *str = "Hello, weird world\n";
        func(str);
    }
    The compiler will complain about incompatible pointers, but it is "just a warning", and in MANY cases, it will do exactly what you (sort of) expect it to do - because you are passing the address to a string - it just so happens that part of the time, you are treating it as a pointer to float.

    Just because something works, and produces the correct result, doesn't mean that it's right. In some systems, this wouldn't work, and it certainly will confuse just about every programmer even on the systems where it does work - and writing confusing code is certainly not good if you want to do it for a living and you want your peers and managers to like you.

    I personally recommend using -Wall -Wextra -Werror when compiling (assuming gcc, corresponding warning levels/"make warnings into errors" are available on most other compilers).


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed