473,387 Members | 1,493 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,387 software developers and data experts.

recasting a pointer

Hi

I was wondering if it is possible to recast a pointer in C ?
What I mean in detail is that I have compressed data coming from
an instrument. These are mostly sample differences from the
previous sample value. If there has been a small change in the
value of the new sample compared with a previous one, then
only one byte is enough to represent that value. If slightly
greater difference between the two sample value, data is
represented with two bytes and 3 bytes for even larger and
so on.
Now I have a pointer which is declared void :
(void *sample_difference)
and want to recast it to the appropriate type eg. (short *).
But it doesn't work. The compiler complains about a void pointer
can not point to another type. So I use "realloc" to try to
recast the pointer and no change is taking place. The pointer still
remains a void and the resulting value assignment is wrong.

I want to avoid to declare four different variables for
different sample values. Is it possible to only have one
variable and then recast it to the proper type ?
Thanks in advance

Kamran

Nov 14 '05 #1
4 6563
On Tue, 23 Dec 2003 04:20:29 -0500, kamran wrote:
Hi

I was wondering if it is possible to recast a pointer in C ? What I mean
in detail is that I have compressed data coming from an instrument.
These are mostly sample differences from the previous sample value. If
there has been a small change in the value of the new sample compared
with a previous one, then only one byte is enough to represent that
value. If slightly greater difference between the two sample value,
data is represented with two bytes and 3 bytes for even larger and so
on.
Now I have a pointer which is declared void : (void *sample_difference)
and want to recast it to the appropriate type eg. (short *). But it
doesn't work. The compiler complains about a void pointer can not point
to another type.


Post some code. Are you doing something like:

void
myfn(void *sample_difference)
{
short *s = sample_difference;

/* use s; this should work */
}

Mike
Nov 14 '05 #2
ka****@uio.no wrote:
Hi

I was wondering if it is possible to recast a pointer in C ?
What I mean in detail is that I have compressed data coming from
an instrument. These are mostly sample differences from the
previous sample value. If there has been a small change in the
value of the new sample compared with a previous one, then
only one byte is enough to represent that value. If slightly
greater difference between the two sample value, data is
represented with two bytes and 3 bytes for even larger and
so on.
Now I have a pointer which is declared void :
(void *sample_difference)
and want to recast it to the appropriate type eg. (short *).
But it doesn't work. The compiler complains about a void pointer
can not point to another type.
There's no obvious reason why the compiler would complain about this.
Conversion from void * to any other object pointer type is allowed.
However, it could fail at runtime if the alignment is incorrect. You
cannot, in general, take any random memory location and interpret it as
an arbitrary type. Some types may need to be aligned on particular
addresses - for example, a two-byte short might need to appear at an
even address.
So I use "realloc" to try to
recast the pointer and no change is taking place. The pointer still
remains a void and the resulting value assignment is wrong.
I don't understand this at all.

I want to avoid to declare four different variables for
different sample values. Is it possible to only have one
variable and then recast it to the proper type ?


A variable never changes its type. That's simply not possible in C. A
cast expression only translates a value to a different type - the source
object is unaffected.

Your description leaves out many details, but my guess is that you need
something like this:

#include <limits.h> /* for CHAR_BIT */

unsigned long value;
int i;

/* Read n bytes: */
value = 0;
for (i=0; i<n; ++i)
{
value = (value << CHAR_BIT) | GetNextByte();
}

/* Now use value somehow. */

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #3
Kevin Goodsell <us*********************@neverbox.com> writes:
[...]
There's no obvious reason why the compiler would complain about
this. Conversion from void * to any other object pointer type is
allowed. However, it could fail at runtime if the alignment is
incorrect. You cannot, in general, take any random memory location and
interpret it as an arbitrary type. Some types may need to be aligned
on particular addresses - for example, a two-byte short might need to
appear at an even address.


One obvious workaround for this is to use memcpy().

For example:

void *ptr = some_value;
short s;

/* s = *((short*)ptr); */
memcpy(&s, ptr, sizeof(short));

In the assignment statement that I commented out, ptr is converted
from void* to short*, and the result is dereferenced, yielding a short
value which is assigned to s. However, this will invoke undefined
behavior if ptr isn't properly aligned. (For example, if ptr points
to an odd address, you're likely to get a bus error, but this is
implementation-specific; all you really need to know is Don't Do That.)

The memcpy() call does essentially the same thing, but it operates
byte-by-byte, so it won't have problems with misaligned pointers.
(The implementation of memcpy() might be optimized to copy data a word
at a time if it's safe to do so.)

This assumes that the data ptr points to has the correct byte
ordering. If a short is 2 bytes long, the high-order and low-order
bytes can appear in either order. This can be an issue if you're
copying data from one platform to another. If you read and write the
data on a single system, you probably don't have to worry about this.

A digression: When I wrote "all you really need to know is Don't Do
That", I meant that that's all you need to know to write correct code.
If programming were just about writing correct code, though, we'd all
have a lot more free time. C's "undefined behavior" covers any
possible or impossible behavior, but knowing something about the
implementation-specific details can be extremely useful in tracking
down bugs. (For the most part, those details are best discussed in
implementation-specific newsgroups, though.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
(Note new e-mail address)
Nov 14 '05 #4


thank you all for the help.

Kamran

Nov 14 '05 #5

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

Similar topics

1
by: Ben Nguyen | last post by:
Im trying to port a hard drive driver that was originally for a different compiler than the one Im using now. The line that doesnt compile with the new compiler is: ActualPartRecord =...
4
by: Carsten Spieß | last post by:
Hello all, i have a problem with a template constructor I reduced my code to the following (compiled with gcc 2.7.2) to show my problem: // a base class class Base{}; // two derived...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
3
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
35
by: tuko | last post by:
Hello kind people. Can someone explain please the following code? /* Create Storage Space For The Texture */ AUX_RGBImageRec *TextureImage; /* Line 1*/ /* Set The Pointer To NULL...
16
by: junky_fellow | last post by:
According to Section A6.6 Pointers and Integers (k & R) " A pointer to one type may be converted to a pointer to another type. The resulting pointer may cause addressing exceptions if the...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
16
by: aegis | last post by:
Given the following: int a = 10; int *p; void *p1; unsigned char *p2; p = &a;
9
by: Phil | last post by:
I have a custom class that has been assigned to a variable and worked with, I now need to cast this class as a different class, which is in a separate assembly, not currently in the project. ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.