Disabling "incompatible pointer type" warning.

Discussion in 'C++' started by PraZ, Mar 29, 2006.

  1. PraZ

    PraZ Guest

    Hi all.

    Here is a simple code, which when compiled with gcc results in the
    warning "incompatible pointer type" for arg 1, as expected. But this is
    just what I want to do, because it makes it easy for me to handle the
    single dimensional stream I have as a multidimensional array inside the
    function func(). Now, I am just wondering if there is a way by which I
    can disable this incompatible pointer type warning in gcc. Any
    suggestions?

    Best regards,
    Prasanna.

    /*The Code*/
    #include <stdio.h>

    void func(char [2][2]);

    int main()
    {
    char* src;
    int i;

    src = (char*)malloc(16);

    for (i=0; i<16; i++)
    src = i;

    func(src+4);

    return 1;
    }

    void func(char src[2][2])
    {
    int r, c;

    for (r = 0; r < 2; r++)
    {
    for (c = 0; c < 2; c++)
    {
    printf("%d, ", src[r][c]);
    }
    printf("\n");
    }
    }
     
    PraZ, Mar 29, 2006
    #1
    1. Advertising

  2. On 29 Mar 2006 01:55:06 -0800, "PraZ" <>
    wrote:

    >Hi all.
    >
    >Here is a simple code, which when compiled with gcc results in the
    >warning "incompatible pointer type" for arg 1, as expected. But this is
    >just what I want to do, because it makes it easy for me to handle the
    >single dimensional stream I have as a multidimensional array inside the
    >function func(). Now, I am just wondering if there is a way by which I
    >can disable this incompatible pointer type warning in gcc. Any
    >suggestions?
    >
    >Best regards,
    >Prasanna.
    >
    >/*The Code*/
    >#include <stdio.h>
    >
    >void func(char [2][2]);
    >
    >int main()
    >{
    > char* src;
    > int i;
    >
    > src = (char*)malloc(16);
    >
    > for (i=0; i<16; i++)
    > src = i;
    >
    > func(src+4);
    >
    >return 1;
    >}
    >
    >void func(char src[2][2])
    >{
    > int r, c;
    >
    > for (r = 0; r < 2; r++)
    > {
    > for (c = 0; c < 2; c++)
    > {
    > printf("%d, ", src[r][c]);
    > }
    > printf("\n");
    > }
    >}


    A cast is necessary:
    func((char (*)[2])src+4);

    But why do you need to do it this way? Also, you leak memory in main.
    And it's not C++ code if you are using malloc and free.

    --
    Bob Hairgrove
     
    Bob Hairgrove, Mar 29, 2006
    #2
    1. Advertising

  3. "PraZ" <> wrote in message
    news:...
    : Here is a simple code, which when compiled with gcc results in the
    : warning "incompatible pointer type" for arg 1, as expected. But this is
    : just what I want to do, because it makes it easy for me to handle the
    : single dimensional stream I have as a multidimensional array inside the
    : function func(). Now, I am just wondering if there is a way by which I
    : can disable this incompatible pointer type warning in gcc. Any
    : suggestions?
    ....
    : void func(char [2][2]);
    ....
    : char* src;
    ....
    : func(src+4);

    You can explicitly cast the parameter to the appropriate type:
    func( (char(*)[2]) (src+4) );

    But to me, it looks like func() might well be inapropriately
    exposing implementation details in its interface.
    If any 4-char sequence is acceptable as an input, the interface
    should not expose a [2][2] bidimensional array to its callers.

    If it is really useful for the implementation, func() could
    create a local variable of the desired type:
    void func( char const src[4] ) // include const if not modified
    {
    char (*tab)[2] = (char(*)[2])src;
    ...use tab as needed...
    }


    Regards -Ivan
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
    Brainbench MVP for C++ <> http://www.brainbench.com
     
    Ivan Vecerina, Mar 29, 2006
    #3
  4. PraZ

    Me Guest

    PraZ wrote:
    > Here is a simple code, which when compiled with gcc results in the
    > warning "incompatible pointer type" for arg 1, as expected. But this is
    > just what I want to do, because it makes it easy for me to handle the
    > single dimensional stream I have as a multidimensional array inside the
    > function func(). Now, I am just wondering if there is a way by which I
    > can disable this incompatible pointer type warning in gcc. Any
    > suggestions?
    >
    > void func(char src[2][2])
    > {
    > for (int r = 0; r < 2; r++) {
    > for (int c = 0; c < 2; c++) {
    > printf("%d, ", src[r][c]);
    > }
    > printf("\n");
    > }
    > }
    >
    > char *src = (char*)malloc(16);
    > func(src+4);


    Working with multidimentional arrays in C/C++ really sucks and it's
    made worse by the fact that doing a cast like what people in this
    thread suggest leads to undefined behavior due to aliasing. The most
    flexible and least annoying way to work with multidimentional arrays is
    to just bite the bullet and do it yourself:

    void func(T *arr, size_t width, size_t height, size_t pitch)
    {
    for (size_t h = 0; h < height; ++h) {
    for (size_t w = 0; w < width; ++w) {
    do_stuff(arr[w]);
    }
    arr += pitch;
    }
    }

    Or a few variations:
    - you can get rid of pitch and just use the width, I don't recommend
    this because it's extremely useful for handling smaller slices of a
    larger matrix.
    - you can make the pitch variable signed (like ptrdiff_t) so you can
    pass it negative values for it to travel upwards instead of downwards.
    I don't really like this but I have seen people use it when working
    with bitmaps with the y-axis pointing the opposite direction than what
    they were expecting.
     
    Me, Mar 29, 2006
    #4
  5. PraZ

    Rolf Magnus Guest

    PraZ wrote:

    > Hi all.
    >
    > Here is a simple code, which when compiled with gcc results in the
    > warning "incompatible pointer type" for arg 1, as expected. But this is
    > just what I want to do, because it makes it easy for me to handle the
    > single dimensional stream I have as a multidimensional array inside the
    > function func(). Now, I am just wondering if there is a way by which I
    > can disable this incompatible pointer type warning in gcc.


    By writing correct code?

    > Any suggestions?
    >
    > Best regards,
    > Prasanna.
    >
    > /*The Code*/
    > #include <stdio.h>
    >
    > void func(char [2][2]);


    This is equivalent to:

    void func(char (*)[2]);

    i.e. a function that takes a pointer to an array of 2 chars.

    > int main()
    > {
    > char* src;
    > int i;
    >
    > src = (char*)malloc(16);


    Don't use malloc. Use new[].

    Btw: you forgot to #include the header that contains the declaration for
    malloc(): stdlib.h

    >
    > for (i=0; i<16; i++)
    > src = i;
    >
    > func(src+4);


    Your dynamically allocated memory is never freed.

    > return 1;


    Why 1? Standard C++ defines three possible return values, which are 0,
    EXIT_SUCCESS (being equivalent to 0) and EXIT_FAILURE. Most systems support
    more than that, but typically, anything except 0 means failure.

    > }


    I'd probably do:

    int main()
    {
    char (*src)[2] = new char[8][2];

    for (int i=0; i<8; i++)
    {
    src[0] = i*2;
    src[1] = i*2+1;
    }

    func(src+2);

    delete [] src;

    return 0;
    }

    > void func(char src[2][2])
    > {
    > int r, c;
    >
    > for (r = 0; r < 2; r++)
    > {
    > for (c = 0; c < 2; c++)
    > {
    > printf("%d, ", src[r][c]);
    > }
    > printf("\n");
    > }
    > }
     
    Rolf Magnus, Mar 29, 2006
    #5
  6. PraZ

    Fei Liu Guest

    "Me" <> wrote in message
    > Working with multidimentional arrays in C/C++ really sucks and it's
    > made worse by the fact that doing a cast like what people in this
    > thread suggest leads to undefined behavior due to aliasing. The most
    > flexible and least annoying way to work with multidimentional arrays is
    > to just bite the bullet and do it yourself:
    >
    > void func(T *arr, size_t width, size_t height, size_t pitch)
    > {
    > for (size_t h = 0; h < height; ++h) {
    > for (size_t w = 0; w < width; ++w) {
    > do_stuff(arr[w]);
    > }
    > arr += pitch;
    > }
    > }
    >
    > Or a few variations:
    > - you can get rid of pitch and just use the width, I don't recommend
    > this because it's extremely useful for handling smaller slices of a
    > larger matrix.
    > - you can make the pitch variable signed (like ptrdiff_t) so you can
    > pass it negative values for it to travel upwards instead of downwards.
    > I don't really like this but I have seen people use it when working
    > with bitmaps with the y-axis pointing the opposite direction than what
    > they were expecting.
    >


    Use std::valarray, boost::multiarray, blitz::array. These implementations
    all support multi dimensional arrays.
     
    Fei Liu, Mar 30, 2006
    #6

  7. > Why 1? Standard C++ defines three possible return values, which are
    > 0,
    > EXIT_SUCCESS (being equivalent to 0) and EXIT_FAILURE. Most systems
    > support
    > more than that, but typically, anything except 0 means failure.


    Because of the memory leak? ;)
     
    Gernot Frisch, Mar 30, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael

    incompatible pointer type warning ?

    Michael, Jun 5, 2006, in forum: C Programming
    Replies:
    8
    Views:
    535
    Ian Collins
    Jun 5, 2006
  2. Replies:
    2
    Views:
    1,182
    Ian Collins
    Jul 26, 2006
  3. Replies:
    0
    Views:
    1,050
  4. Replies:
    1
    Views:
    513
    Andrew Poelstra
    Jul 26, 2006
  5. DFouton
    Replies:
    1
    Views:
    1,019
    DFouton
    Nov 14, 2010
Loading...

Share This Page