Solved

Convert unsigned char** to unsigned char*

Posted on 2003-12-02
3
705 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

828 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