473,398 Members | 2,188 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,398 software developers and data experts.

type casting question

raj
Hi,

I am a beginner and need help with the following:

'ifr_data' is (char *)
'args'***** is* unsigned long args[4]

((unsigned long *)(&ifr.ifr_data))[0] = (unsigned long)args;

What does the above statement do.?
Why should we type cast args again it is already of type 'unsigned
long.'
Why do we need to pass the address of ifr_data and later dereference,
if
I understood corrctly, using [].

Cant we do something like:
(unsigned long *) ifr.ifr_data = args

COMPLETE CODE: (remember 'ifr.ifr_data' is char *)
---------------
int br_device_ioctl32(struct bridge *br, unsigned long arg0, unsigned
long arg1, unsigned long arg2, unsigned long arg3)
{
unsigned long args[4];
********struct ifreq ifr;

********args[0] = arg0;
********args[1] = arg1;
********args[2] = arg2;
********args[3] = arg3;

********memcpy(ifr.ifr_name, br->ifname, IFNAMSIZ);
********((unsigned long *)(&ifr.ifr_data))[0] = (unsigned long)args;

********return ioctl(br_socket_fd, SIOCDEVPRIVATE, &ifr);
}
Nov 13 '05 #1
4 5148
ma*******@yahoo.com (raj) wrote:
'ifr_data' is (char *)
'args'***** is* unsigned long args[4]

((unsigned long *)(&ifr.ifr_data))[0] = (unsigned long)args;

What does the above statement do.?
Bugger all useful. It casts the address of args to an unsigned long,
then assigns the unsigned-long-was-address to the memory officially
occupied by the char * called ifr_data . The way this is done is not
guaranteed to work (think bus errors), and where it does, is not
guaranteed to result in useful values.
Why should we type cast args again it is already of type 'unsigned
long.'
args is not of type unsigned long. It's an array.
Why do we need to pass the address of ifr_data and later dereference,
if I understood corrctly, using [].
You're not passing anything. You're casting the address of ifr_data to a
type it does not really have: ifr.ifrdata is treated as if _it_ is an
unsigned long, which is, clearly, wrong.
As for the [0], you don't _really_ need them, you can also use a *. In
fact, that's probably slightly(!) clearer:
*((unsigned long *)(&ifr.ifr_data)) = (unsigned long)args;
Cant we do something like:
(unsigned long *) ifr.ifr_data = args
No. For one thing, you cannot assign to a cast expression, because a
cast delivers a simple value, not an lvalue.
COMPLETE CODE: (remember 'ifr.ifr_data' is char *)
---------------
int br_device_ioctl32(struct bridge *br, unsigned long arg0, unsigned
long arg1, unsigned long arg2, unsigned long arg3)
{
unsigned long args[4];
********struct ifreq ifr;

********args[0] = arg0;
********args[1] = arg1;
********args[2] = arg2;
********args[3] = arg3;

********memcpy(ifr.ifr_name, br->ifname, IFNAMSIZ);
********((unsigned long *)(&ifr.ifr_data))[0] = (unsigned long)args;

********return ioctl(br_socket_fd, SIOCDEVPRIVATE, &ifr);
}


This is system-specific code. I suspect the secret to that odd
assignment lies in what ioctl() expects in its third parameter. I also
suspect that this was written for a system that does, in fact, make the
guarantees I told you above C itself does not make. Finally, I suspect
this code was not exactly meant for beginners, and suggest you don't
worry if you don't understand it yet.

Richard
Nov 13 '05 #2
raj wrote:

Hi,

I am a beginner and need help with the following:

'ifr_data' is (char *)
'args' is unsigned long args[4]

((unsigned long *)(&ifr.ifr_data))[0] = (unsigned long)args;

What does the above statement do.?


This produces undefined behavior because ifr_data is not guaranteed to
be properly aligned to a ulong type. What you probably want to do is
write a small function or macro which puts the correct bytes in ifr_data
by shifting and masking args.

/david

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown
Nov 13 '05 #3

On Tue, 9 Sep 2003, raj wrote:

I am a beginner and need help with the following:

'ifr_data' is (char *)
'args'***** is* unsigned long args[4]

((unsigned long *)(&ifr.ifr_data))[0] = (unsigned long)args;

What does the above statement do.?
Assigns 'args' to 'ifr.ifr_data', but in a roundabout way that
introduces undefined behavior. *However*, on systems where this
sort of thing does "work," it probably won't change the bit
representation of 'args' during the assignment.

Standard C (no undefined behavior):

ifr.ifr_data = (char *) args;

Standard C (maybe closer to intended effect; has UB):

char *temp = args;
memcpy(&ifr.ifr_data, temp, sizeof temp);

Why should we type cast args again it is already of type 'unsigned
long.'
'args' is *not* of type 'unsigned long'. You told us it was of
type 'unsigned long [4]'; which, in this context, decays to a
pointer to the first element in the array -- that is, to a pointer
to unsigned long ('unsigned long *'). That's not the same thing
as 'unsigned long'.
Why do we need to pass the address of ifr_data and later dereference,
if I understood corrctly, using [].
You don't "pass" the address of 'ifr.ifr_data' anywhere. You "compute"
it. And see below.
Cant we do something like:
(unsigned long *) ifr.ifr_data = args


No. Try that on a conforming compiler, and read the error message
you get.
The '(unsigned long *)' tells the compiler to calculate the value
gotten by treating 'ifr.ifr_data' as an unsigned long. For example,
that value might be 0x55D06A32. Then you're asking the compiler to
assign something to that value. *That doesn't make any sense!*
You can't assign to a cast-expression any more than you can assign
to an addition-expression like '1+1'.

<snip code>

I recommend you try the first alternative method I proposed, and
see whether it works. (But if this is a serious project, be extra-
careful to document the change, and make sure you can tell whether
it's working or not! Just because X compiles doesn't mean X will
work.)

HTH,
-Arthur

Nov 13 '05 #4
raj <ma*******@yahoo.com> wrote:
Hi,

I am a beginner and need help with the following:

'ifr_data' is (char *)
'args' is unsigned long args[4]

((unsigned long *)(&ifr.ifr_data))[0] = (unsigned long)args;

What does the above statement do.?
The above statement is nonsense. It tries to access a "char *" object
as if it were an "unsigned long", and it casts an (unsigned long *) to
(unsigned long).

The correct way to do what it appears to be *trying* to do is much
simpler:

ifr.ifr_data = (char *)args;
Why should we type cast args again it is already of type 'unsigned
long.'
No it isn't - args is of type ``unsigned long [4]''. In an expression
where it's not the operand of either the unary-& or sizeof operators,
it is evaluated as a pointer to the first element of the array, and the
resulting value has type ``unsigned long *''.
Why do we need to pass the address of ifr_data and later dereference,
if I understood corrctly, using [].
Because it wants to pretend to the compiler that ifr_data is an unsigned
long (which is a silly thing to do anyway).
Cant we do something like:
(unsigned long *) ifr.ifr_data = args


No, the result of a cast operator is not an lvalue, so it can't be
assigned to. That's just as nonsensical as writing:

a + 2 = 10;

- Kevin.

Nov 13 '05 #5

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

Similar topics

1
by: maxim vexler | last post by:
in a book i am ready now : O'Reilly - Web Database Application with PHP and MySQL, 2nd ed. by David Lane, Hugh E. Williams on chapter 9 the author give an example for age validation :...
7
by: Richard Myers | last post by:
Hello. I am getting an InvalidCastException which has revealed yet more of my ignorance. I cant believe i dont already know this and haven't encountered it before until now. I am consuming a...
4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
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) { ...
3
by: Steve Teeples | last post by:
I have a method that passes in two string objects (both numerical numbers) and a string identifying their type. public bool DoCompare(string num1, string num2, string theirType) { System.Type...
10
by: Bob | last post by:
This has been bugging me for a while now. GetType isn't availble for variables decalred as interface types, I have to DirectCast(somevariable, Object). In example: Sub SomeSub(ByVal...
3
by: FB's .NET Dev PC | last post by:
I'm trying to create a general-purpose routine to update a value in a data location on a remote server. The client-server protocol is OPC, but that isn't immediately important to my question. The...
7
by: Martin Robins | last post by:
I am currently looking to be able to read information from Active Directory into a data warehouse using a C# solution. I have been able to access the active directory, and I have been able to return...
15
by: shuisheng | last post by:
Dear All, Assume I have a class named Obj. class Obj { }; And a class named Shape which is derived from Obj. class Shape: public Obj
4
by: Schkhan | last post by:
Hi, I am sturggling with a peace of code mainly its about casting a pointer to one type of structure to a pointer to another type of structure. the confusion begins with the following...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
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
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...
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,...
0
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...

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.