471,066 Members | 1,005 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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

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[i][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;
}

Sep 14 '07 #1
9 5697
mathieu wrote:
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[i][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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 14 '07 #2
mathieu wrote:
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[i][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
Sep 14 '07 #3
On Sep 14, 5:42 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
mathieu wrote:
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[i][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[i][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

Sep 14 '07 #4
On Sep 14, 6:01 pm, Barry <dhb2...@gmail.comwrote:
mathieu wrote:
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[i][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

Sep 14 '07 #5
mathieu wrote:
On Sep 14, 6:01 pm, Barry <dhb2...@gmail.comwrote:
>mathieu wrote:
>>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[i][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
--
Thanks
Barry
Sep 14 '07 #6
mathieu wrote:
[..]
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[i][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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 14 '07 #7
On Sep 14, 6:26 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
mathieu wrote:
[..]
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.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 15 '07 #8
mathieu wrote:
On Sep 14, 6:01 pm, Barry <dhb2...@gmail.comwrote:
>mathieu wrote:
>>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[i][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

--
Thanks
Barry
Sep 15 '07 #9
On Sep 14, 6:10 pm, mathieu <mathieu.malate...@gmail.comwrote:
On Sep 14, 6:01 pm, Barry <dhb2...@gmail.comwrote:
mathieu wrote:
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[i][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).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 15 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Jim Langston | last post: by
9 posts views Thread by Gregory.A.Book | last post: by
reply views Thread by leo001 | last post: by

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.