473,327 Members | 2,055 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,327 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 6286
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Bushido Hacks | last post by:
Hey c.l.c++ and/or c.g.a.opengl posters, How do I convert a hexidecimal string, traditionally used for defining colors with HTML, into a floating point array? In other words, how do I convert...
5
by: Code4u | last post by:
In the course of writing numerical code I needed to convert a float to an int with a defined behavior: if the float is great than INT_MAX, set the int to INT_MAX, otherwise assign directly. The...
2
by: Benjamin Rutt | last post by:
Does anyone have C code laying around to do this? I have to read in some binary data files that contains some 4-byte IBM/370 floating point values. I would like a function to convert 4-byte...
3
by: Jim Langston | last post by:
I have a CSkill class which is rather complex as it is recursive. That is: class CSkill { public: CSkill( std::string Name, float Value ): Name_( Name ), Value_( Value ) {}; void Update(...
9
by: Gregory.A.Book | last post by:
I am interested in converting sets of 4 bytes to floats in C++. I have a library that reads image data and returns the data as an array of unsigned chars. The image data is stored as 4-byte floats....
2
by: CoreyWhite | last post by:
Problem: You have numbers in string format, but you need to convert them to a numeric type, such as an int or float. Solution: You can do this with the standard library functions. The...
12
by: joestevens232 | last post by:
Okay, Im having some problems with my code. Im trying to use the <cstdlib> library and im trying to convert string data at each whitespace slot. I think if you see my code you'll get what im trying...
8
by: Ruben | last post by:
error: passing `const Weight' as `this' argument of `float Weight::wgt()' discards qualifiers seems to be some sort of standard error format that I'm not understanding. I have code that...
7
by: ma740988 | last post by:
Consider the equation (flight dynamics stuff): Yaw (Degrees) = Azimuth Angle(Radians) * 180 (Degrees) / 3.1415926535897932384626433832795 (Radians) There's a valid reason to use single...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.