Using Double pointer

S

Sarath

Why the following happening?

UINT m_uArrXpos[4][7];

UINT **p = m_uArrXposSchema; // Failed to compile

UINT **p = (UINT**)m_uArrXposSchema; // Success compilation
 
I

Ian Collins

Sarath said:
Why the following happening?

UINT m_uArrXpos[4][7];

UINT **p = m_uArrXposSchema; // Failed to compile
What ever "m_uArrXposSchema" is, it isn't a "UINT**" (whatever "UINT" is).
UINT **p = (UINT**)m_uArrXposSchema; // Success compilation
You lied to the compiler.
 
S

Sarath

Sarath said:
Why the following happening?
UINT m_uArrXpos[4][7];
UINT **p = m_uArrXposSchema; // Failed to compile

What ever "m_uArrXposSchema" is, it isn't a "UINT**" (whatever "UINT" is).
UINT **p = (UINT**)m_uArrXposSchema; // Success compilation

You lied to the compiler.

Thanks Collins for your reply.

BTW UINT defined as

typedef unsigned int UINT;

What i've lied to compiler? It's a two dimensional array no?

Could you please be more specific?
 
S

Sarath

Sarath said:
Why the following happening?
UINT m_uArrXpos[4][7];
UINT **p = m_uArrXposSchema; // Failed to compile

What ever "m_uArrXposSchema" is, it isn't a "UINT**" (whatever "UINT" is).
UINT **p = (UINT**)m_uArrXposSchema; // Success compilation

You lied to the compiler.

Another funniest thing is that,

UINT m_uArrXpos[4][7];
UINT **p = m_uArrXpos; // Failed to compile
UINT **p = (UINT**)m_uArrXpos; // Success compilation
UINT **p = static_cast<UINT**>(m_uArrXpos); // Failed to compile

What it makes in difference static_cast and C style cast?

I'm using Visual C++ 6.0 Compiler
 
S

Sylvester Hesp

Sarath wrote :
Sarath said:
Why the following happening?
UINT m_uArrXpos[4][7];
UINT **p = m_uArrXposSchema; // Failed to compile

What ever "m_uArrXposSchema" is, it isn't a "UINT**" (whatever "UINT" is).
UINT **p = (UINT**)m_uArrXposSchema; // Success compilation

You lied to the compiler.

Another funniest thing is that,

UINT m_uArrXpos[4][7];
UINT **p = m_uArrXpos; // Failed to compile
UINT **p = (UINT**)m_uArrXpos; // Success compilation
UINT **p = static_cast<UINT**>(m_uArrXpos); // Failed to compile

What it makes in difference static_cast and C style cast?

I'm using Visual C++ 6.0 Compiler

The difference is that static_cast is safe, while a C style cast can
also be a reinterpret_cast (and possibly a const_cast).

UINT* is a pointer that points to UINT. We could do
typedef UINT* UINTPTR;
to create a typedef for such a pointer.

Now, what is UINTPTR*? A pointer that points to UINTPTR. And since
UINTPTR is itself a pointer to UINT, that makes UINTPTR* the same as
UINT**, or a pointer that points to [a pointer that points to UINT].
You can use it as a two-dimensional array, but then it's representation
would be an array of pointers that point to arrays of UINTs. E.g., p[0]
is a pointer of type UINT* that points to an array of UINTs, p[1] is
also a pointer, etc.

m_uArrXpos is an array of 4x7 UINTs. It's not an array of pointers that
point to UINT. Therefore you cannot assign a UINT[4][7] to a UINT*. You
can convert it to a (UINT*)[7] though (which is a pointer that points
to an array of 7 UINTs)

- Sylvester
 
A

Amal P

Sarath said:
Why the following happening?
UINT m_uArrXpos[4][7];
UINT **p = m_uArrXposSchema; // Failed to compile
What ever "m_uArrXposSchema" is, it isn't a "UINT**" (whatever "UINT" is).
You lied to the compiler.

Another funniest thing is that,

UINT m_uArrXpos[4][7];
UINT **p = m_uArrXpos; // Failed to compile
UINT **p = (UINT**)m_uArrXpos; // Success compilation
UINT **p = static_cast<UINT**>(m_uArrXpos); // Failed to compile

What it makes in difference static_cast and C style cast?

I'm using Visual C++ 6.0 Compiler

Dear Sarath,

You are using a two dimensional array and not a pointer array. A
two dimensional array dont have a "**". Suppose the location given in
m_uArrXpos is 0x0012fef4. the value at this location will not be an
address. Insted it will be a value of the m_uArrXpos[0][0], So
defenitly a pointer is made to an invalid location. Even though the C
style cast works it will be a junk pointer. You cant use it out.
Compiler prevents this casting untill and otherwise you force to do
the casting.

Thanks,
Amal P.
 
S

Sarath

Sarath wrote:
Why the following happening?
UINT m_uArrXpos[4][7];
UINT **p = m_uArrXposSchema; // Failed to compile
What ever "m_uArrXposSchema" is, it isn't a "UINT**" (whatever "UINT" is).
UINT **p = (UINT**)m_uArrXposSchema; // Success compilation
You lied to the compiler.
Another funniest thing is that,
UINT m_uArrXpos[4][7];
UINT **p = m_uArrXpos; // Failed to compile
UINT **p = (UINT**)m_uArrXpos; // Success compilation
UINT **p = static_cast<UINT**>(m_uArrXpos); // Failed to compile
What it makes in difference static_cast and C style cast?
I'm using Visual C++ 6.0 Compiler

Dear Sarath,

You are using a two dimensional array and not a pointer array. A
two dimensional array dont have a "**". Suppose the location given in
m_uArrXpos is 0x0012fef4. the value at this location will not be an
address. Insted it will be a value of the m_uArrXpos[0][0], So
defenitly a pointer is made to an invalid location. Even though the C
style cast works it will be a junk pointer. You cant use it out.
Compiler prevents this casting untill and otherwise you force to do
the casting.

Thanks,
Amal P.- Hide quoted text -

- Show quoted text -

Hmmm... Seems like to learn everything from start :D
 
S

Salt_Peter

Sarath wrote:
Why the following happening?
UINT m_uArrXpos[4][7];
UINT **p = m_uArrXposSchema; // Failed to compile
What ever "m_uArrXposSchema" is, it isn't a "UINT**" (whatever "UINT" is).
UINT **p = (UINT**)m_uArrXposSchema; // Success compilation
You lied to the compiler.
--
Ian Collins.
Another funniest thing is that,
UINT m_uArrXpos[4][7];
UINT **p = m_uArrXpos; // Failed to compile
UINT **p = (UINT**)m_uArrXpos; // Success compilation
UINT **p = static_cast<UINT**>(m_uArrXpos); // Failed to compile
What it makes in difference static_cast and C style cast?
I'm using Visual C++ 6.0 Compiler
Dear Sarath,
You are using a two dimensional array and not a pointer array. A
two dimensional array dont have a "**". Suppose the location given in
m_uArrXpos is 0x0012fef4. the value at this location will not be an
address. Insted it will be a value of the m_uArrXpos[0][0], So
defenitly a pointer is made to an invalid location. Even though the C
style cast works it will be a junk pointer. You cant use it out.
Compiler prevents this casting untill and otherwise you force to do
the casting.
Thanks,
Amal P.- Hide quoted text -
- Show quoted text -

Hmmm... Seems like to learn everything from start :D

You mean unlearn. You are being confused because of too much code
hiding the details about the type involved. If you declare a 2D array
of unsigned integers, the elements are unsigned integers, not
pointers.

It is recommended not to hide pointers behind decorations. You'll find
some platforms that use these decorations to isolate your code from
any future changes they make as well as hide their implementations.
That doesn't mean you should be hiding your code from yourself.

#include <iostream>

typedef unsigned UINT; // no problem
typedef UINT* PTR; // this is ugly
typedef UINT** PTRPTR; // this is mischievious

int main()
{
// a container of primitives
UINT Arr[2][2];
Arr[0][0] = 99;
Arr[1][1] = 88;

unsigned* p = &Arr[0][0];
std::cout << *p << std::endl; // 99
p = &Arr[1][1];
std::cout << *p << std::endl; // 88

// a container of pointers
PTR Pointers[2][2]; // all point to nothing
Pointers[0][0] = &Arr[0][0]; // ok
unsigned* pu = Pointers[0][0];
std::cout << *pu << std::endl; // 99

// a container of pointers to pointers
PTRPTR PtrPtr[2][2]; // all point to invalid pointers
PtrPtr[0][0] = &Pointers[0][0]; // ok, ptr set to a valid ptr
unsigned** ppu = PtrPtr[0][0];
std::cout << **ppu << std::endl; // 99
}
 
A

Andre Kostur

Why the following happening?

UINT m_uArrXpos[4][7];

UINT **p = m_uArrXposSchema; // Failed to compile

Because m_uArrXposSchema "decays" into a simple UINT*. You can't simply
assign a UINT* to a UINT**.
UINT **p = (UINT**)m_uArrXposSchema; // Success compilation

You forcibly casted the pointer into a UINT**. You told the compiler to
"shut-up, I know what I'm doing".
 

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,294
Messages
2,571,511
Members
48,198
Latest member
couriermedicine

Latest Threads

Top