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 Language

Converting pointers


I need to re-structure some code in a program I wrote som time ago

I'd like to reuse my code instead of having to rewrite the lot. That
leads me to my question.

I have the following two pointers:

        char    ***pppValues;
        char    **ppValues;

The relation between the two is:

        ppValues = *pppValues;

Now I need to rewrite the following statement:

        ppValues[index]

So that I have a similar statement with the same value/meaning, but it
must be rewritten to use pppValues instead of ppValues.

I have tried the following statements without luck.

        *pppValues[index]
        *(pppValues[index])
        *(pppValues)[index]
        *((pppValues)[index])
        (*pppValues)[index]
        *((*pppValues)+index)

None of which provides me with the correct result.

I don't usually have problem coverting my pointers, but im tired and
can't think straight right now.

I'd be very greatful if someone takes the time to help me, as I've
stared myself blind, without getting the correct result.

If i'm som lucky that someone can spot the problem right away, I'd also
like the following statement rewritten. But it ought to be quite easy if
the statement above has been correctly rewritten.

        *(unsigned long *)ppValues[index]

Thanks a lot in advance
/Andreas Vinther, Denmark.

That's the right one.

> (...)
> None of which provides me with the correct result.
> I don't usually have problem coverting my pointers, but im tired and
> can't think straight right now.

You are indeed:-)

Forget that this is about pointers and think about syntax instead.

   [] takes the address from the single expression in front, and adds
   the index inside the []'s.  The ()'s ensure that whatever is inside
   them are that single expression, just like if it were a variable with
   the value of that expression.
   So (*pppValues)[index] == ppValues[index].  That's just syntax.

Maybe you initialized one of the values wrong or something.
E.g. with '==' instead of '='.

>    *(unsigned long *)ppValues[index]

Why do you want to access a pointer as if it were an unsigned long?

--
Regards,
Hallvard

On Sun, 27 May 2007 16:33:12 +0200, Andreas Vinther <vint@it.dk>
wrote:

This is not a statement.  At best, it is an expression.

>So that I have a similar statement with the same value/meaning, but it
>must be rewritten to use pppValues instead of ppValues.

>I have tried the following statements without luck.

>    *pppValues[index]
>    *(pppValues[index])
>    *(pppValues)[index]
>    *((pppValues)[index])

These do not work because [] has higher precedence than *.  As a
result, the * operand is being applied to pppValues[index], which may
not even exist.

>    (*pppValues)[index]

Here, the parentheses insure that the * is applied to pppValues and
the [] to the result, which should be ppValues.  If this did not
produce the correct result, then your assertion about the relation
between the two is incorrect.  Show us your real code and tell us how
the actual result differs from your expectations.

>    *((*pppValues)+index)

This is by definition identical to the previous expression.

>None of which provides me with the correct result.

>I don't usually have problem coverting my pointers, but im tired and
>can't think straight right now.

>I'd be very greatful if someone takes the time to help me, as I've
>stared myself blind, without getting the correct result.

>If i'm som lucky that someone can spot the problem right away, I'd also
>like the following statement rewritten. But it ought to be quite easy if
>the statement above has been correctly rewritten.

>    *(unsigned long *)ppValues[index]

ppValues is a char**.  ppValues[index] is a char*.  It points to some
byte in memory.  If that byte is suitably aligned for an unsigned
long, it is legal to cast the char* to unsigned long*.  If the bit
pattern starting at this byte and extending for sizeof(unsigned long)
bytes contains a valid value for an unsigned long, it is legal to
dereference the converted pointer value to access the integer value it
points to.

If that byte is not suitably aligned, the cast invokes undefined
behavior.  If the bit pattern is invalid, the dereference invokes
undefined (I think) behavior.  This last may be only implementation
defined behavior but in either case the expression will probably not
evaluate to the value you are expecting.

As noted above, the substitution for ppValues[index] should be
((*pppvalues)[index]) making the entire expression
     *(unsigned log*)((*pppvalues)[index])
What problem are you experiencing?  Show us your real code.

Remove del for email

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc