Solved

Convert unsigned char** to unsigned char*

Posted on 2003-12-02
3
703 Views
Last Modified: 2012-06-27
unsigned char **test;
unsigned char *result = test;

I want single pointer result to have same content as double pointer test.

The question is how can I typecast double pointer to single pointer.

Thanks
0
Comment
Question by:neel3333
3 Comments
 
LVL 11

Expert Comment

by:bcladd
ID: 9861528
You can't BUT you can have it point at the same spot test points:

result = *test;

Note that this will point at the exact same memory and you should copy it if you are going to play games with it.

Hope this helps,
-bcl
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 500 total points
ID: 9862311
test is a pointer to a pointer or an array of pointers, i. e. test is an address in memory where a pointer to some other memory is allocated. result is a pointer to an unsigned char or to an array of unsigned chars. So, at first address you find pointers that point somewhere else and second you find the characters themselves.

if you assign the first pointer of test to result as in

    unsigned char **test;
    unsigned char *result = test;

you get lost of one dimension. The only case where this makes sense is if test is an array of pointers that point to contiguous memory, i. e. when test is a 2D field and the pointers point to the rows of the 2D field.

   unsigned char  matrix[10][20];
   unsigned char **test = matrix;
   unsigned char *result = (unsigned char*)matrix;

Hope that helps, Alex




0
 
LVL 17

Expert Comment

by:rstaveley
ID: 9863253
itsmeandnobodyelse's 2D matrix is a good example of a valid cast. However, C-style casts can hide a multitude of sins, and we should really use reinterpret_cast<> to confess our sins in C++, when we are saying to the compiler that the programmer knows best.

        unsigned char matrix[10][20];
        unsigned char *result1 = static_cast<unsigned char*>(matrix); /* The compiler throws an error - this *isn't* a straight-forward cast! */
        unsigned char *result2 = reinterpret_cast<unsigned char*>(matrix); /* This compiles, but doesn't it make you feel evil? reinterpret_cast<> says that the programmer knows what he is doing*/
        unsigned char *result3 = (unsigned char*)matrix; /* Sneaky C-style cast. Bitter experience gets these to work */
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now