Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Convert unsigned char** to unsigned char*

Posted on 2003-12-02
3
Medium Priority
?
713 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 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

926 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