Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old March 29th, 2006, 10:05 AM
PraZ
Guest
 
Posts: n/a
Default Disabling "incompatible pointer type" warning.

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] = 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");
}
}

  #2  
Old March 29th, 2006, 12:05 PM
Bob Hairgrove
Guest
 
Posts: n/a
Default Re: Disabling "incompatible pointer type" warning.

On 29 Mar 2006 01:55:06 -0800, "PraZ" <prasanna.sethuraman@patni.com>
wrote:
[color=blue]
>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] = 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");
> }
>}[/color]

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
NoSpamPlease@Home.com
  #3  
Old March 29th, 2006, 12:05 PM
Ivan Vecerina
Guest
 
Posts: n/a
Default Re: Disabling "incompatible pointer type" warning.

"PraZ" <prasanna.sethuraman@patni.com> wrote in message
news:1143626106.137290.294600@e56g2000cwe.googlegr oups.com...
: 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


  #4  
Old March 29th, 2006, 01:45 PM
Me
Guest
 
Posts: n/a
Default Re: Disabling "incompatible pointer type" warning.

PraZ wrote:[color=blue]
> 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);[/color]

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.

  #5  
Old March 29th, 2006, 06:05 PM
Rolf Magnus
Guest
 
Posts: n/a
Default Re: Disabling "incompatible pointer type" warning.

PraZ wrote:
[color=blue]
> 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.[/color]

By writing correct code?
[color=blue]
> Any suggestions?
>
> Best regards,
> Prasanna.
>
> /*The Code*/
> #include <stdio.h>
>
> void func(char [2][2]);[/color]

This is equivalent to:

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

i.e. a function that takes a pointer to an array of 2 chars.
[color=blue]
> int main()
> {
> char* src;
> int i;
>
> src = (char*)malloc(16);[/color]

Don't use malloc. Use new[].

Btw: you forgot to #include the header that contains the declaration for
malloc(): stdlib.h
[color=blue]
>
> for (i=0; i<16; i++)
> src[i] = i;
>
> func(src+4);[/color]

Your dynamically allocated memory is never freed.
[color=blue]
> return 1;[/color]

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.
[color=blue]
> }[/color]

I'd probably do:

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

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

func(src+2);

delete [] src;

return 0;
}
[color=blue]
> 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");
> }
> }[/color]

  #6  
Old March 30th, 2006, 01:15 AM
Fei Liu
Guest
 
Posts: n/a
Default Re: Disabling "incompatible pointer type" warning.


"Me" <anti_spam_email2003@yahoo.com> wrote in message[color=blue]
> 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.
>[/color]

Use std::valarray, boost::multiarray, blitz::array. These implementations
all support multi dimensional arrays.


  #7  
Old March 30th, 2006, 09:45 AM
Gernot Frisch
Guest
 
Posts: n/a
Default Re: Disabling "incompatible pointer type" warning.

[color=blue]
> 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.[/color]

Because of the memory leak? ;)



 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,338 network members.