Solved

Convert unsigned char** to unsigned char*

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

757 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

16 Experts available now in Live!

Get 1:1 Help Now