converting 'float (*)[4]' to 'const float**'

M

mathieu

Hi,

I know I am doing something stupid here, but it's friday night and I
cannot see what is the issue here:

Thanks,
-Mathieu

#include <iostream>

void print(const float **a,unsigned int X,unsigned int Y)
{
for(unsigned int i=0; i < X; ++i)
for(unsigned int j=0; j < Y; ++j)
std::cout << a[j] << std::endl;
}

int main()
{
const unsigned int X = 2;
const unsigned int Y = 4;
static const float a[X][Y] = {
{10,9,380.033,17189.4, },
{16,10,308.615,17542.4, }
};

print((const float**)a,X,Y);

return 0;
}
 
V

Victor Bazarov

mathieu said:
I know I am doing something stupid here, but it's friday night and I
cannot see what is the issue here:

Thanks,
-Mathieu

#include <iostream>

void print(const float **a,unsigned int X,unsigned int Y)

This function expects 'a' to be an array of pointers to const float.
{
for(unsigned int i=0; i < X; ++i)
for(unsigned int j=0; j < Y; ++j)
std::cout << a[j] << std::endl;
}

int main()
{
const unsigned int X = 2;
const unsigned int Y = 4;
static const float a[X][Y] = {
{10,9,380.033,17189.4, },
{16,10,308.615,17542.4, }
};

print((const float**)a,X,Y);


Here you pass an _array_of_arrays_ *pretending* it's an array of
pointers. That's a VERY BAD IDEA(tm).
return 0;
}

What you _could_ do (not that it's a good idea, but still), is to
create an array of pointers and pass it into 'print':

const float *aa[] = { a[0], a[1] };
print(aa, X, Y);

V
 
B

Barry

mathieu said:
Hi,

I know I am doing something stupid here, but it's friday night and I
cannot see what is the issue here:

Thanks,
-Mathieu

#include <iostream>

void print(const float **a,unsigned int X,unsigned int Y)
{
for(unsigned int i=0; i < X; ++i)
for(unsigned int j=0; j < Y; ++j)
std::cout << a[j] << std::endl;
}

int main()
{
const unsigned int X = 2;
const unsigned int Y = 4;
static const float a[X][Y] = {
{10,9,380.033,17189.4, },
{16,10,308.615,17542.4, }
};

print((const float**)a,X,Y);

return 0;
}


a better way to rewrite your program is

template <class T, unsigned X, unsigned Y>
void print(T const (&a)[X][Y])
{
}
 
M

mathieu

mathieu said:
I know I am doing something stupid here, but it's friday night and I
cannot see what is the issue here:

#include <iostream>
void print(const float **a,unsigned int X,unsigned int Y)

This function expects 'a' to be an array of pointers to const float.


{
for(unsigned int i=0; i < X; ++i)
for(unsigned int j=0; j < Y; ++j)
std::cout << a[j] << std::endl;
}

int main()
{
const unsigned int X = 2;
const unsigned int Y = 4;
static const float a[X][Y] = {
{10,9,380.033,17189.4, },
{16,10,308.615,17542.4, }
};
print((const float**)a,X,Y);

Here you pass an _array_of_arrays_ *pretending* it's an array of
pointers. That's a VERY BAD IDEA(tm).


return 0;
}

What you _could_ do (not that it's a good idea, but still), is to
create an array of pointers and pass it into 'print':

const float *aa[] = { a[0], a[1] };
print(aa, X, Y);


Waw, I never realized that before... Thanks a bunch for the lesson.
I'll work around the issue this way (Muuuuhahhahha):

#include <iostream>

template <typename T>
void print(const T a,unsigned int X,unsigned int Y)
{
for(unsigned int i=0; i < X; ++i)
for(unsigned int j=0; j < Y; ++j)
std::cout << a[j] << std::endl;
}

int main()
{
const unsigned int X = 2;
const unsigned int Y = 4;
static const float a[X][Y] = {
{10,9,380.033,17189.4, },
{16,10,308.615,17542.4, }
};

print(a,X,Y);

return 0;
}


Sorry
 
M

mathieu

mathieu said:
I know I am doing something stupid here, but it's friday night and I
cannot see what is the issue here:

#include <iostream>
void print(const float **a,unsigned int X,unsigned int Y)
{
for(unsigned int i=0; i < X; ++i)
for(unsigned int j=0; j < Y; ++j)
std::cout << a[j] << std::endl;
}

int main()
{
const unsigned int X = 2;
const unsigned int Y = 4;
static const float a[X][Y] = {
{10,9,380.033,17189.4, },
{16,10,308.615,17542.4, }
};
print((const float**)a,X,Y);
return 0;
}

a better way to rewrite your program is

template <class T, unsigned X, unsigned Y>
void print(T const (&a)[X][Y])
{

}


Thanks Barry,

Why do you use a reference in this case since you know you are
passing a pointer-to-pointer ?

Thanks
-Mathieu
 
B

Barry

mathieu said:
mathieu said:
Hi,
I know I am doing something stupid here, but it's friday night and I
cannot see what is the issue here:
Thanks,
-Mathieu
#include <iostream>
void print(const float **a,unsigned int X,unsigned int Y)
{
for(unsigned int i=0; i < X; ++i)
for(unsigned int j=0; j < Y; ++j)
std::cout << a[j] << std::endl;
}
int main()
{
const unsigned int X = 2;
const unsigned int Y = 4;
static const float a[X][Y] = {
{10,9,380.033,17189.4, },
{16,10,308.615,17542.4, }
};
print((const float**)a,X,Y);
return 0;
}

a better way to rewrite your program is

template <class T, unsigned X, unsigned Y>
void print(T const (&a)[X][Y])
{

}


Thanks Barry,

Why do you use a reference in this case since you know you are
passing a pointer-to-pointer ?


it's reference to const T[X][Y],
read the declaration like this:

a is a reference to array(with 2D [X][Y]) of const T
 
V

Victor Bazarov

mathieu said:
[..]
template <typename T>
void print(const T a,unsigned int X,unsigned int Y)

The top-level 'const' is superfluous. For an exercise, print out
'typeid(T).name()' and 'typeid(a).name()' inside the function.
{
for(unsigned int i=0; i < X; ++i)
for(unsigned int j=0; j < Y; ++j)
std::cout << a[j] << std::endl;
}

int main()
{
const unsigned int X = 2;
const unsigned int Y = 4;
static const float a[X][Y] = {
{10,9,380.033,17189.4, },
{16,10,308.615,17542.4, }
};

print(a,X,Y);

return 0;
}



V
 
J

James Kanze

mathieu said:
[..]
template <typename T>
void print(const T a,unsigned int X,unsigned int Y)
The top-level 'const' is superfluous. For an exercise, print
out 'typeid(T).name()' and 'typeid(a).name()' inside the
function.

Not always. It's ignored by typeid, and when considering the
function *declaration* (i.e. f(int); and f( int const ); declare
the same function), but it *is* significant in the function
body.
 
B

Barry

mathieu said:
mathieu said:
Hi,
I know I am doing something stupid here, but it's friday night and I
cannot see what is the issue here:
Thanks,
-Mathieu
#include <iostream>
void print(const float **a,unsigned int X,unsigned int Y)
{
for(unsigned int i=0; i < X; ++i)
for(unsigned int j=0; j < Y; ++j)
std::cout << a[j] << std::endl;
}
int main()
{
const unsigned int X = 2;
const unsigned int Y = 4;
static const float a[X][Y] = {
{10,9,380.033,17189.4, },
{16,10,308.615,17542.4, }
};
print((const float**)a,X,Y);
return 0;
}

a better way to rewrite your program is

template <class T, unsigned X, unsigned Y>
void print(T const (&a)[X][Y])
{

}


Thanks Barry,

Why do you use a reference in this case since you know you are
passing a pointer-to-pointer ?


Oh, my. I always don't get the idea of the OP

I meant you could pass the array, not casting it to float** then pass it
 
J

James Kanze

mathieu said:
I know I am doing something stupid here, but it's friday night and I
cannot see what is the issue here:
#include <iostream>
void print(const float **a,unsigned int X,unsigned int Y)
{
for(unsigned int i=0; i < X; ++i)
for(unsigned int j=0; j < Y; ++j)
std::cout << a[j] << std::endl;
}
int main()
{
const unsigned int X = 2;
const unsigned int Y = 4;
static const float a[X][Y] = {
{10,9,380.033,17189.4, },
{16,10,308.615,17542.4, }
};
print((const float**)a,X,Y);
return 0;
}

a better way to rewrite your program is
template <class T, unsigned X, unsigned Y>
void print(T const (&a)[X][Y])
{
}

Why do you use a reference in this case since you know you are
passing a pointer-to-pointer ?

You're not passing a pointer to a pointer. First, because you
don't have a pointer to a pointer anywhere in your program;
that's why it wasn't working in the first place. You have an
array of arrays, which will convert implicitly to a pointer to
an array in many contexts (but not all), but never to a pointer
to a pointer (which would be an entirely different data
structure). Second, and that's the critical aspect here, when
binding to a reference, an array only converts implicitly to a
pointer if the reference type is pointer (and the results of the
conversion won't bind to a non-const reference). What happens
here is that the compiler does template argument deduction,
given an array float[2][4], deduces that T is float, X is 2
and Y is 4, instantiates the template with these arguments, and
then binds the array to the reference. The nice part about it
is that the compiler does all of the work; you don't have to
worry about passing the dimension arguments (and maybe getting
them wrong). The bad part about it is that the compiler can
only work with what it knows; you can only pass a C style array
(which hasn't been converted to a pointer), which means that the
dimensions must be compile time constants. Another potentially
bad aspect is that the compiler instantiates a different
function for each set of dimensions, which can lead to code
bloat (although that's almost certainly not an issue for such a
small function as this).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top