473,893 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Typecasting pointers

Hi All,

Is it valid in C to typecast a pointer?

eg. code snippet... considering int as 16 bit and long as 32 bit.

int *variable, value;

*((long*)variab le)++ = value;
*((long*)variab le)++ = value;
*((long*)variab le)++ = value;
*((long*)variab le)++ = value;

Thanks,
Nishu

Jan 29 '07 #1
26 12577
Nishu wrote:
Hi All,

Is it valid in C to typecast a pointer?
If alignment requirements are met, then yes. However, with a few
exceptions, all you are allowed to do with the result is convert it
back to the original type.
eg. code snippet... considering int as 16 bit and long as 32 bit.

int *variable, value;

*((long*)variab le)++ = value;
*((long*)variab le)++ = value;
*((long*)variab le)++ = value;
*((long*)variab le)++ = value;
This is not allowed. Even when you are able to convert 'variable' to a
pointer-to-long, you may not use this pointer to access anything that
isn't really a long.

Jan 29 '07 #2


On Jan 29, 2:59 pm, "Harald van Dijk" <true...@gmail. comwrote:
Nishu wrote:
Hi All,
Is it valid in C to typecast a pointer?If alignment requirements are met, then yes. However, with a few
exceptions, all you are allowed to do with the result is convert it
back to the original type.
eg. code snippet... considering int as 16 bit and long as 32 bit.
int *variable, value;
*((long*)variab le)++ = value;
*((long*)variab le)++ = value;
*((long*)variab le)++ = value;
*((long*)variab le)++ = value;This is not allowed. Even when you are able to convert 'variable' to a
pointer-to-long, you may not use this pointer to access anything that
isn't really a long.
In case value is long...
long value;

I'm getting warnings on my compiler.."obje cts that have been cast are
not l-value."

Thanks,
Nishu

Jan 29 '07 #3
"Nishu" <na**********@g mail.comwrites:
On Jan 29, 2:59 pm, "Harald van Dijk" <true...@gmail. comwrote:
>Nishu wrote:
Hi All,
Is it valid in C to typecast a pointer?If alignment requirements are met, then yes. However, with a few
exceptions, all you are allowed to do with the result is convert it
back to the original type.
eg. code snippet... considering int as 16 bit and long as 32 bit.
int *variable, value;
*((long*)variab le)++ = value;
*((long*)variab le)++ = value;
*((long*)variab le)++ = value;
*((long*)variab le)++ = value;
This is not allowed. Even when you are able to convert 'variable' to a
pointer-to-long, you may not use this pointer to access anything that
isn't really a long.

In case value is long...
long value;

I'm getting warnings on my compiler.."obje cts that have been cast are
not l-value."
Take out the UB caused by the cast to long * by replacing it with int *
and you should get the same message. It describes exactly and
succinctly what is wrong. The result of a cast is not an lvalue -- it
never denotes an object that can be changed. In your case,

*((int *)variable)++

would try to increment something, but the result of a cast is never a
thing that can be incremented, no matter what the type is used in the
cast. You should get the message from the more obvious:

int x;
((int)x)++; /* error... casts do not make lvalues */

--
Ben.
Jan 29 '07 #4
Nishu wrote:
Is it valid in C to typecast a pointer?
Aside: it's not "typecast". It's just "cast".

--
Chris "electric hedgehog" Dollin
"Who do you serve, and who do you trust?" /Crusade/

Jan 29 '07 #5
Nishu said:
Hi All,

Is it valid in C to typecast a pointer?
It's rarely wise, and often not valid.
eg. code snippet... considering int as 16 bit and long as 32 bit.

int *variable, value;

*((long*)variab le)++ = value;
Let's count the problems.

Firstly, value is indeterminate, so you can't use its value legitimately.
The behaviour is undefined if you try.

Secondly, even if that weren't a problem, variable is indeterminate, so you
can't use its value legitimately. The behaviour is undefined if you try.

Thirdly, even if those weren't problems, variable is an int *, and (if its
value isn't indeterminate or a null pointer) it points to an int object,
which may not be aligned correctly for a long int.

Fourthly, even if those weren't problems, a cast-expression such as
(long *)variable yields a value, not an object, and you can't use ++ on a
mere value. It requires an object.

Sort out those problems and then ask again.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 29 '07 #6


On Jan 29, 4:18 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
Nishu said:
Hi All,
Is it valid in C to typecast a pointer?It's rarely wise, and often not valid.
eg. code snippet... considering int as 16 bit and long as 32 bit.
int *variable, value;
*((long*)variab le)++ = value;Let's count the problems.
Sort out those problems and then ask again.
Hi,
I want to cast my 16bit pointer as 32 bit pointer for copying
operation, after that i need 16bit pointer operations only. here's the
test version of what I need to do..

#include<stdio. h>
#include<stdlib .h>

int main(void)
{
unsigned short value;
long lvalue;

short *ptr, *ptr1;

ptr = (short*) malloc(sizeof(s hort) * 8);

ptr1 = ptr;
value = 0xFFF0;

lvalue = (unsigned short)value | ((unsigned short)value << 16);

*((long*)ptr)++ = lvalue; /* is it portable? */
*((long*)ptr)++ = lvalue;
*((long*)ptr)++ = lvalue;
*((long*)ptr)++ = lvalue;

free (ptr1);
return 0;
}
MSVC doesn't give error here, but I'm doubtful about its portability.
Please help me about other possible loop holes too.

Thanks,
Nishu

Jan 29 '07 #7


On Jan 29, 4:46 pm, "Nishu" <naresh.at...@g mail.comwrote:
>Hi,
I want to cast my 16bit pointer as 32 bit pointer for copying
operation, after that i need 16bit pointer operations only. here's the
test version of what I need to do..
here's lil' correction... and one more doubt.

#include<stdio. h>
#include<stdlib .h>

int main(void)
{
short value; /* it is signed actually*/
long lvalue;

short *ptr, *ptr1;

ptr = malloc(sizeof(s hort) * 8);

ptr1 = ptr;
value = 0xFFF0; /* i get warning here. I don't understand why.
warning is
'=' : truncation from 'const int ' to 'short' */

lvalue = (unsigned short)value | ((unsigned short)value << 16);

*((long*)ptr)++ = lvalue;
*((long*)ptr)++ = lvalue;
*((long*)ptr)++ = lvalue;
*((long*)ptr)++ = lvalue;

free (ptr1);
return 0;
}

Thanks,
Nishu

Jan 29 '07 #8
Nishu wrote:
On Jan 29, 4:46 pm, "Nishu" <naresh.at...@g mail.comwrote:
>>Hi,
I want to cast my 16bit pointer as 32 bit pointer for copying
operation,
/Why/ do you want to do this bizarre thing?
>after that i need 16bit pointer operations only. here's the
test version of what I need to do..

here's lil' correction... and one more doubt.

#include<stdio. h>
#include<stdlib .h>

int main(void)
{
short value; /* it is signed actually*/
long lvalue;

short *ptr, *ptr1;

ptr = malloc(sizeof(s hort) * 8);
Better is likely to be:

ptr = malloc( 8 * sizeof( *ptr ) );

If you want 8 things. (Below, it looks like you might really
want 4).
ptr1 = ptr;
value = 0xFFF0; /* i get warning here. I don't understand why.
warning is
'=' : truncation from 'const int ' to 'short' */
Well, yes. You say your `short`s are 16 bits. 0xFFF0 is a positive
value (there are no negative literals),and it's an `int`. It won't fit
into a signed short. Your compiler is warning you that you're trying
to stuff an `int` into a `short` and that you may well have lost
information. (Opinions about the values of such a message vary.)
lvalue = (unsigned short)value | ((unsigned short)value << 16);
Why not declare `value` as an `unsigned short`, since that's all you
ever use it as? Come to that, why not assign a literal directly to
`lvalue` and not bother with `value` at all?
*((long*)ptr)++ = lvalue;
*((long*)ptr)++ = lvalue;
*((long*)ptr)++ = lvalue;
*((long*)ptr)++ = lvalue;
Similarly, if what you want to do is assign to successive `long`s
starting at the mallocated address, why not do the obvious, which
is

long *ptr = malloc( 4 * sizeof( *ptr ) );
...
*ptr++ = lvalue;
*ptr++ = lvalue;
*ptr++ = lvalue;
*ptr++ = lvalue;

or even (having declared `int i`):

for (i = 0; i < 4; i += 1) ptr[i] = lvalue;
free (ptr1);
return 0;
}
I don't think you're telling us everything you need to.

--
Chris "electric hedgehog" Dollin
"It took a very long time, much longer than the most generous estimates."
- James White, /Sector General/

Jan 29 '07 #9
Nishu wrote:
>
On Jan 29, 4:46 pm, "Nishu" <naresh.at...@g mail.comwrote:
>Hi,
I want to cast my 16bit pointer as 32 bit pointer for copying
operation, after that i need 16bit pointer operations only. here's the
test version of what I need to do..

here's lil' correction... and one more doubt.

#include<stdio. h>
#include<stdlib .h>

int main(void)
{
short value; /* it is signed actually*/
long lvalue;

short *ptr, *ptr1;

ptr = malloc(sizeof(s hort) * 8);

ptr1 = ptr;
value = 0xFFF0; /* i get warning here. I don't understand why.
warning is
'=' : truncation from 'const int ' to 'short' */

lvalue = (unsigned short)value | ((unsigned short)value << 16);

*((long*)ptr)++ = lvalue;
*((long*)ptr)++ = lvalue;
*((long*)ptr)++ = lvalue;
*((long*)ptr)++ = lvalue;

free (ptr1);
return 0;
}

Thanks,
Nishu
1>------ Build started: Project: Test, Configuration: Debug Win32 ------
1>Compiling...
1>test.c
1>c:\visual studio 2005\projects\t est\test\test.c (43) : warning C4213:
nonstandard extension used : cast on l-value
1>c:\visual studio 2005\projects\t est\test\test.c (44) : warning C4213:
nonstandard extension used : cast on l-value
1>c:\visual studio 2005\projects\t est\test\test.c (45) : warning C4213:
nonstandard extension used : cast on l-value
1>c:\visual studio 2005\projects\t est\test\test.c (46) : warning C4213:
nonstandard extension used : cast on l-value
1>c:\visual studio 2005\projects\t est\test\test.c (43) : *warning C6011:
Dereferencing NULL pointer '((long *)ptr)++': Lines: 29, 30, 32, 34, 36,
37, 41, 43*

Looks ominous to me!

1>Linking...
1>Embedding manifest...
1>Test - 0 error(s), 5 warning(s)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
Jan 29 '07 #10

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

Similar topics

13
2445
by: brian | last post by:
Quick question: if I have a structure: struct foo { unsigned char *packet; unsigned char *ip_src; };
12
4493
by: bwaichu | last post by:
What is the best way to handle this warning: warning: cast from pointer to integer of different size I am casting in and out of a function that requires a pointer type. I am casting an integer as a pointer, but the pointer is 8 bytes while the integer is only 4 bytes. Here's an example function:
0
9982
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9830
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10469
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8021
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5858
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6066
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4684
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4280
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3289
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.