Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C++ Programming

Using Double pointer


Why the following happening?

UINT m_uArrXpos[4][7];

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

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

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.

On May 29, 5:27 pm, Ian Collins <ian-n@hotmail.com> wrote:

> 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?

On May 29, 5:27 pm, Ian Collins <ian-n@hotmail.com> wrote:

> 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

Sarath <CSar@gmail.com> writes:
>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/language/c/ptrtut/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.
Sarath wrote :

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

On May 29, 5:40 pm, Sarath <CSar@gmail.com> wrote:

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.

On May 29, 6:39 pm, Amal P <enjoyam@gmail.com> wrote:

Hmmm... Seems like to learn everything from start :D
On May 29, 6:04 am, Sarath <CSar@gmail.com> wrote:

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

Sarath <CSar@gmail.com> wrote 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".
Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc