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

casting trouble

Hello folks,

During developping I've met a problem this is how it looks like

I have an expression like this

(type *)var + 1

Wath heppens is I want to make a pointer to this

sth like ptr = &((type *)var + 1)
I know this is meaningless but I don't have the idea in mind

please could you give a suggestion ?

thanks
Nov 14 '05 #1
5 1168
macluvitch wrote:
Hello folks,

During developping I've met a problem this is how it looks like

I have an expression like this

(type *)var + 1 Assuming var is a pointer, I don't understand you are casting like
this (invites trouble, for sure).

Wath heppens is I want to make a pointer to this

sth like ptr = &((type *)var + 1)
You are incrementing the pointer variable by a scalar, (which sounds
fine). Why would you get the address of a pointer variable ?
That is UB. Did u want to do a dereferencing out here.
I know this is meaningless but I don't have the idea in mind

please could you give a suggestion ?


Please let us know the objetive clearly.

--
Karthik
Humans please 'removeme_' for my real email.
Nov 14 '05 #2
"macluvitch" <ma********@hotmail.com> wrote:
I have an expression like this

(type *)var + 1

Wath heppens is I want to make a pointer to this

sth like ptr = &((type *)var + 1)
I know this is meaningless but I don't have the idea in mind


Well, it can't be done that way. You cannot take the address of an
expression result; expression results have no definite address in
memory. If you really need a pointer that points at that value, you'll
need to create an object of the appropriate type, assign the value to
that object, then get its address. Like this:

compatible_type var;
type *dummy, **ptr;

var=somevalue;

dummy=((type *)var + 1);
ptr=&dummy;

Since the value of a pointer does not depend on the value of what it
points at (think about it: does your address depend on your name? If you
change your name by deed poll, does your address change?), this is
correct, too:

compatible_type var;
type *dummy, **ptr=&dummy;

var=somevalue;

dummy=((type *)var + 1);

dummy needs to be of type "type *", because that is the type of the
cast, and therefore of the whole expression. ptr needs to be of type
"type **", because it points at dummy.

Richard
Nov 14 '05 #3
Thank you folks this sounds to be meaningfull :)

I've tried this before posting but this should be a bit slow
so I was thinking of a small hack , I mean some way to do it like any
simple pointer affectation like I've mentioned before
Anyhow, how this will probably work great why troubling oneself

Ok thank you folks this is very kind of you
Oh I've another question (sorry)
The casting is done by the compiler or the assembler
(no optimizing option is turning on)
90% it should be done by the compiler what do you think ?

bye

Nov 14 '05 #4
"macluvitch" <ma********@hotmail.com> wrote:
I've tried this before posting but this should be a bit slow
How do you know? Have you measured it? Was the difference significant?
Does it occur often enough to have a measurable influence on your
program, or are you just worrying prematurely?
Ok thank you folks this is very kind of you
Oh I've another question (sorry)
The casting is done by the compiler or the assembler


That question does not make sense in the context of ISO C. There's no
reason to assume the existence of an assembler.

Richard
Nov 14 '05 #5
I've got a look at gcc qasembly output . I think the casting is done by gcc

Nov 14 '05 #6

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

Similar topics

5
by: Vinodh Kumar | last post by:
I see that casting changes the value of a pointer in case of multiple inheritance.In single inheritance also it is the same know?Isn't it? Vinodh Kumar P
4
by: coeng | last post by:
I am having trouble comprehending the use of the four casting operators in C++. Does anyone know of a URL that provides a very detailed, yet very simple, explanation of what they accomplish and...
4
by: David Rager | last post by:
Howdy, Put briefly, I have an array of chars, which I would like to access in pairs of bytes via casting the array to an array of shorts. I'm trying to be as elegant as possible. Below is a...
7
by: Jakob Bieling | last post by:
Hi, I have a question about casting and using the casted pointer: Suppose I have a 'base' class and a 'derived' class (which is derived from 'base'). Now I have this scenario: base* p1 = new...
35
by: ytrama | last post by:
Hi, I have read in one of old posting that don't cast of pointer which is returned by the malloc. I would like to know the reason. Thanks in advance, YTR
6
by: Jim Bancroft | last post by:
Hi everyone, I'm having some trouble with the code below. I receive a compile-time error on the second line saying "; expected": private static void myTestFunction(long myLong) { ...
20
by: quantumred | last post by:
I found the following code floating around somewhere and I'd like to get some comments. unsigned char a1= { 5,10,15,20}; unsigned char a2= { 25,30,35,40}; *(unsigned int *)a1=*(unsigned int...
7
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
3
by: newbtemple | last post by:
Hey all, having some trouble with casting values inputed into a text box into double. In particular, I'm having trouble with the decimal. It's ok if someone puts in a number with a decimal when...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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: 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
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
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.