float m[4][3];

X

xontrn

For

float a[9];

we know just using the array name a gives a float* to the first
element in the array.

For

float m[4][3];

what is the type of the array name m?
 
V

Victor Bazarov

For

float a[9];

we know just using the array name a gives a float* to the first
element in the array.

For

float m[4][3];

what is the type of the array name m?

Since 'm' is an array of arrays, the array-to-pointer conversion of
'm' would yield a pointer to an array.

V
 
X

xontrn

float a[9];
we know just using the array name a gives a float* to the first
element in the array.

float m[4][3];
what is the type of the array name m?

Since 'm' is an array of arrays, the array-to-pointer conversion of
'm' would yield a pointer to an array.

Okay, then why doesn't this work? Isn't T* d[3] a pointer to an
array?

#include <iostream>
using namespace std;

template<typename T>
class C
{
public:
C(T* d[3], int m, int n)
{
mData = new T*[m];
for(int i = 0; i < m; ++i)
mData = new T[n];

for(int i = 0; i < r; ++i)
for(int j = 0; j < c; ++j)
mData[j] = d[j];

r = m;
c = n;
}

~C()
{
for(int i = 0; i < r; ++i)
delete[] mData;

delete[] mData;
}

T& operator()(int i, int j)
{
return mData[j];
}

void print()
{
for(int i = 0; i < r; ++i)
{
for(int j = 0; j < c; ++j)
{
cout << data[j] << "\t";
}
cout << endl;
}
}

private:
T** data;
int r;
int c;
};

int main()
{
float m[2][3];
m[0][0] = 1.0f;
m[0][1] = 2.0f;
m[0][2] = 3.0f;
m[1][0] = 4.0f;
m[1][1] = 5.0f;
m[1][2] = 6.0f;
m[2][0] = 7.0f;
m[2][1] = 8.0f;
m[2][2] = 9.0f;

C<float> T(m, 2, 3);

T.print();
}
 
V

Victor Bazarov

float a[9];
we know just using the array name a gives a float* to the first
element in the array.

float m[4][3];
what is the type of the array name m?

Since 'm' is an array of arrays, the array-to-pointer conversion of
'm' would yield a pointer to an array.

Okay, then why doesn't this work? Isn't T* d[3] a pointer to an
array?

No,

T *d[3]

is an array of three pointers. You need to use parentheses.

V
 
X

xontrn

(e-mail address removed) wrote:
For
float a[9];
we know just using the array name a gives a float* to the first
element in the array.
For
float m[4][3];
what is the type of the array name m?
Since 'm' is an array of arrays, the array-to-pointer conversion of
'm' would yield a pointer to an array.
Okay, then why doesn't this work? Isn't T* d[3] a pointer to an
array?

No,

T *d[3]

is an array of three pointers. You need to use parentheses.

It still didn't work. How did you do the parentheses?
 
I

Ivan Vecerina

: > >>> float m[4][3];
: >
: > >>> what is the type of the array name m?
: >
: > >> Since 'm' is an array of arrays, the array-to-pointer conversion
of
: > >> 'm' would yield a pointer to an array.
: >
: > > Okay, then why doesn't this work? Isn't T* d[3] a pointer to an
: > > array?
: >
: > No,
: >
: > T *d[3]
: >
: > is an array of three pointers. You need to use parentheses.
:
: It still didn't work. How did you do the parentheses?

Did you try:
T (*d)[3]


hth -Ivan
 
X

xontrn

Did you try:
T (*d)[3]

Thank you, I was doing it wrong.

So just in how I should read this:

T* d[3] = "d is an array of 3 T*"

T (*d)[3] = "d is a pointer to a T[3]"
 
V

Victor Bazarov

Did you try:
T (*d)[3]

Thank you, I was doing it wrong.

So just in how I should read this:

T* d[3] = "d is an array of 3 T*"

T (*d)[3] = "d is a pointer to a T[3]"

Right. By extension,

T (*d[5])[3] => "d is an array of 5 pointers to T[3]"

V
 

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
474,290
Messages
2,571,453
Members
48,129
Latest member
DianneCarn

Latest Threads

Top