473,387 Members | 1,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

Mar 29 '06 #1
6 5223
On 29 Mar 2006 01:55:06 -0800, "PraZ" <pr*****************@patni.com>
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] = 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
No**********@Home.com
Mar 29 '06 #2
"PraZ" <pr*****************@patni.com> wrote in message
news:11**********************@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
Mar 29 '06 #3
Me
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.

Mar 29 '06 #4
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] = 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[i][0] = i*2;
src[i][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");
}
}


Mar 29 '06 #5

"Me" <an*****************@yahoo.com> 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.
Mar 30 '06 #6
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? ;)

Mar 30 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Silmar | last post by:
Hi! In my form I have table which cells contain input objects of type="text" which initially are disabled. I would like to activate them by clicking on them. However because input object does...
3
by: Jaroslaw Zabiello | last post by:
I got strange errors in Zope 2.7. METALError macro 'context/base' has incompatible version None, at line 1, column 1 One ZPT file (named 'base') defines some simply slots: <html...
12
by: Charlie Zender | last post by:
Hi, I am unable to compile a large body of code with extremely pedantic compile time checks activate, so that warnings cause errors. With GCC 3.3.1, I do this with gcc -std=c99 -pedantic...
18
by: steve | last post by:
I'm trying to create a structure of three pointers to doubles. For which I have: typedef struct { double *lst_t, *lst_vc, *lst_ic; } last_values; I then need to allocate space for...
3
by: Ed L. | last post by:
On 7.4.6, is there any problem with defining one column of a view to be a string literal? For example ... $ psql -c "create view fooview as select 'bar' as footype" WARNING: column "footype"...
8
by: solarin | last post by:
Hi all. I'm writting a logger class to write all the debug/info/warning/error messages in a file. Every time a class needs to send any message, should send a code (int) and a message (string)....
4
by: craig | last post by:
During construction of an object "parent", if you create a subobject that stores a pointer to the parent (through the "this" pointer), will that pointer be valid when the subobject is later called?...
3
by: bxscikid | last post by:
I am using VB 6 in order to clear a listbox (lststuff) containing material entered by the user via input boxes. I am trying to utilize a command button which will clear the list for the user. When...
18
by: Stephan Beal | last post by:
Hi, all! Before i ask my question, i want to clarify that my question is not about the code i will show, but about what the C Standard says should happen. A week or so ago it occurred to me...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.