Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Convert unsigned char** to unsigned char*

Posted on 2003-12-02
3
Medium Priority
?
711 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

718 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