473,396 Members | 2,038 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

cast a pointer

Hi,

I have a block of data that was saved as either unsigned shorts or
unsigned chars. I want to make a single pointer to it so I don't have
to keep casting.

BYTE *pData; // the original data, could be stored as unsigned
shorts or unsigned chars.

// A generic pointer to the data, which I want to interpret
depending on spp:
void *p = NULL;

// Here interpret it differently?
if (nSamplesPerPixel == 1) {
p = (unsigned short *)pData;
}
else if (nSamplesPerPixel == 3) {
p = (unsigned char *)pData;
}

How can I do that?

Thanks

Aug 25 '06 #1
4 1668
markww posted:
Hi,

I have a block of data that was saved as either unsigned shorts or
unsigned chars. I want to make a single pointer to it so I don't have
to keep casting.

I suppose you could start off with something like:

struct Pointer {
enum Type {Char, Short} type;

union {
char unsigned *pc;
short unsigned *ps;
};
};

--

Frederick Gotham
Aug 25 '06 #2

"markww" <ma****@gmail.comwrote in message
news:11********************@75g2000cwc.googlegroup s.com...
Hi,

I have a block of data that was saved as either unsigned shorts or
unsigned chars. I want to make a single pointer to it so I don't have
to keep casting.

BYTE *pData; // the original data, could be stored as unsigned
shorts or unsigned chars.

// A generic pointer to the data, which I want to interpret
depending on spp:
void *p = NULL;

// Here interpret it differently?
if (nSamplesPerPixel == 1) {
p = (unsigned short *)pData;
}
else if (nSamplesPerPixel == 3) {
p = (unsigned char *)pData;
}
Obviously, casting pData and assigning it to a void* isn't gong to do
anything. Drop the cast if you want to use a void*. But that's not what
you want, right?

Have you looked into using templates? They're great for this kind of thing.

-Howard


Aug 25 '06 #3
Frederick Gotham wrote:
markww posted:
>Hi,

I have a block of data that was saved as either unsigned shorts or
unsigned chars. I want to make a single pointer to it so I don't have
to keep casting.


I suppose you could start off with something like:

struct Pointer {
enum Type {Char, Short} type;

union {
char unsigned *pc;
short unsigned *ps;
};
};
CAREFUL. I had to clean up hundreds of lines of bullshit
code that used that construct. If you store a char* into
one pointer and retrieved it from the unsigned, it may not
be converted properly. At least with reinterpret_cast
you get a fighting chance.

The DELELCOR HEP that we ported 4 BSD to encoded the operand
size in the pointer. Our casting operators in the compiler
could convert the pointers appropriately, but the kernel as
written used the "convert-by-union" which led to bizarre
effects.
Aug 26 '06 #4
Frederick Gotham wrote:
markww posted:
>Hi,

I have a block of data that was saved as either unsigned shorts or
unsigned chars. I want to make a single pointer to it so I don't have
to keep casting.


I suppose you could start off with something like:

struct Pointer {
enum Type {Char, Short} type;

union {
char unsigned *pc;
short unsigned *ps;
};
};
class Pointer {
void* internal;
public:
Pointer(char unsigned*);
Pointer(short unsinged*);
char unsigned* get_puc();
short unsigned* get_suc();
};

Implementation is left as an exercise to the student.
Aug 26 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Graham Nicholls | last post by:
Hi. I'm having some fun with numbers. I've extraced an image sizes from a jpeg file img_x,img_y=image.getsize() then I'm trying to use those sizes to scale the image, but because python...
10
by: LaEisem | last post by:
On-the-job, I have "inherited" a lot of old C language software. A question or two about when "casting" of null pointer constants is needed has occurred during behind-the-scenes cleanup of some...
9
by: Frederick Gotham | last post by:
Let's assume that we're working on the following system: CHAR_BIT == 8 sizeof( char* ) == 4 (i.e. 32-Bit) Furthermore, lets assume that the memory addresses are distributed as follows: ...
5
by: Frederick Gotham | last post by:
Before I begin, here's a list of assumptions for this particular example: (1) unsigned int has no padding bits, and therefore no invalid bit- patterns or trap representations. (2) All types have...
7
by: llothar | last post by:
When i use -W4 on visual c 7.0 i get warning C4054 translator1.c(1703) : warning C4054: 'type cast' : from function pointer 'void * (__cdecl *)(se_agent *)' to data pointer 'void *'...
18
by: Felix Kater | last post by:
I haven't been thinking about it for years but recently I've stumbled on the fact that 'casting' is actually doing (at least) two different things: On the one hand 'casting' means: 'Change...
5
by: johnbrown105 | last post by:
Hello All, I did try searching the archives, but the topics seemed to be mostly about base and derived classes. I have reached Chapter 8 in Bruce Eckel's "Thinking in C++ 2nd Edition Vol....
14
by: andreyvul | last post by:
g++ says a reinterpret cast from void* is disallowed; however, all I'm trying to do is de-capsulate a void* argument that holds the pointer to a class (and static_cast forces cast constructor) any...
7
by: lovecreatesbea... | last post by:
Is it always legal to cast expressions of type of multi-dimension array to type of pointer? Including: T to T* , T to T* , T to T* , and so on... For example: int *mtxrot1d(int *p,...
7
by: * Tong * | last post by:
Hi, I couldn't figure out how to properly type cast in this case: $ cat -n type_cast.c 1 #include <stdio.h> 2 3 typedef unsigned char Byte; 4 typedef signed char Small_Int; 5
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.