By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,382 Members | 1,688 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,382 IT Pros & Developers. It's quick & easy.

Using Double pointer

P: n/a
Why the following happening?

UINT m_uArrXpos[4][7];

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

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

May 29 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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.
May 29 '07 #2

P: n/a
On May 29, 5:27 pm, Ian Collins <ian-n...@hotmail.comwrote:
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.
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?

May 29 '07 #3

P: n/a
On May 29, 5:27 pm, Ian Collins <ian-n...@hotmail.comwrote:
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

May 29 '07 #4

P: n/a
Sarath <CS*****@gmail.comwrites:
>What i've lied to compiler? It's a two dimensional array no?
>Could you please be more specific?
http://burks.bton.ac.uk/burks/langua...trtut/ch7x.htm
might help (and in general this kind of problem is more often dealt with in C books/boards than in C++ ones). Two dimensional arrays aren't all the same.
May 29 '07 #5

P: n/a
Sarath wrote :
On May 29, 5:27 pm, Ian Collins <ian-n...@hotmail.comwrote:
>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
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
May 29 '07 #6

P: n/a
On May 29, 5:40 pm, Sarath <CSar...@gmail.comwrote:
On May 29, 5:27 pm, Ian Collins <ian-n...@hotmail.comwrote:
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.

May 29 '07 #7

P: n/a
On May 29, 6:39 pm, Amal P <enjoyam...@gmail.comwrote:
On May 29, 5:40 pm, Sarath <CSar...@gmail.comwrote:


On May 29, 5:27 pm, Ian Collins <ian-n...@hotmail.comwrote:
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

May 29 '07 #8

P: n/a
On May 29, 6:04 am, Sarath <CSar...@gmail.comwrote:
On May 29, 6:39 pm, Amal P <enjoyam...@gmail.comwrote:
On May 29, 5:40 pm, Sarath <CSar...@gmail.comwrote:
On May 29, 5:27 pm, Ian Collins <ian-n...@hotmail.comwrote:
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
}

May 29 '07 #9

P: n/a
Sarath <CS*****@gmail.comwrote in news:1180427071.782839.307170
@n15g2000prd.googlegroups.com:
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".
May 29 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.