casting char pointer to unsigned int

Pages: 12
Jun 23, 2008 at 8:28pm
I have found this forum to be very helpful in my (young) quest for learning C++. I have the following functions that I dont quite understand, what I do understand, I have commented, the rest I am lost on:

unsigned int get_this(unsigned int a,unsigned int b, unsigned char c)
{
unsigned char* ptr = (unsigned char*) &a; //storing a location in ptr
unsigned int ret_val;

//what is the significance of this? if the int is not equal to 4 bytes set it to 0? why? I thought sizeof allocates enough space so that the object may fit.

if (b >= sizeof(unsigned int)) b = 0;

*(ptr + b) = c; //dereferencing pointer to go to the contents of (ptr + b), and store whats in c in that location

ret_val = *((unsigned int*) ptr); //this one lost me.....
return ret_val;
}
Jun 23, 2008 at 8:45pm
First, Please use the code tags when posting code.

Second, why are you working/looking at these functions?
As something learning C++ this isn't typically the sort of code you would be coming across. It's not especially common to be learning byte/bit manipulation as a newbie to C++.

Jun 23, 2008 at 8:46pm
This is extracting the value of one of the four bytes of the unsigned int

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
unsigned int get_this(unsigned int a,unsigned int b, unsigned char c)
{
unsigned char* ptr = (unsigned char*) &a; // By using a char pointer instead 
                                          //  of a int pointer, you limit what                                     
                                          //  you have access to, to one byte
unsigned int ret_val;                     //  We're going to return an int, not 
                                          //   a char

// b is the offset into the 4 bytes of the unsigned int so if its more than 4 just 
// set it to the first byte
if (b >= sizeof(unsigned int)) b = 0;
// as you say
*(ptr + b) = c; //dereferencing pointer to go to the contents of (ptr + b), 
// and store whats in c in that location


// cast the one byte pointed to by pointer back to an int pointer.
// Then set the full 4 bytes of ret_val to the value stored in the single byte
// pointed to by ptr
ret_val = *((unsigned int*) ptr);
return ret_val;
}


I hope this explains it. If not ask the questions here.
Last edited on Jun 23, 2008 at 8:48pm
Jun 23, 2008 at 8:50pm
This is extracting the value of one of the four bytes of the unsigned int

Close :)

This code is modifying one of the bytes in A, at offset B to become value C. And returning the value. So not modifying the original A (as it was never passed in by reference)
Last edited on Jun 23, 2008 at 9:03pm
Jun 23, 2008 at 8:51pm
Zaita,

Sorry for not posting in code tags, I will take care of it from now on. As for byte and bit manipulation, I would like to get a hang of it by looking at functions that are of bit and byte manipulation nature, it may seem odd, but I am perhaps just a wee bit curious?

I would really appreciate if some insight would be given on this function.

Thanks again for your help.
Jun 23, 2008 at 8:52pm
Yep, You're right, got it backwards :/

@ ladesidude
what sort of insight are you after, exactly what its doing, why you would want to do it, or something else?
Last edited on Jun 23, 2008 at 8:55pm
Jun 23, 2008 at 8:54pm
bnbertha/Zaita,

Thanks a lot for making me understand, in simpler words...what is an "offset"? Does it mean one off or something else?

I'm glad I came across this forum.

Thanks again.

Jun 23, 2008 at 8:57pm
Offset is which byte, treating the int like a char array[4] the offset is the array index
Last edited on Jun 23, 2008 at 8:57pm
Jun 23, 2008 at 8:57pm
Ok.

When we think of an unsigned int, we think of a 4 byte value that looks like 0x44332211. 11 being lower in value than 44. When it's stored in memory it looks like 0x11223344. Backwards to how we'd think about it.

What this code does is take a value in c, and place if into the byte (1-4) given by the offset+1.

If we did get_this(1, 0, 0x10). The return value would be 0x01000000. This to us is 0x10 (16). The initial 1 we put in as a is over-written by 0x10.
Last edited on Jun 23, 2008 at 8:58pm
Jun 23, 2008 at 9:01pm
An offset can be regarded as an adjustment value.
So in bnbertha's example, *(ptr + b) = c; b is the offset because it is used to adjust the value of ptr that is dereferenced.
So if b=0 you access *ptr, b=1 you access *(ptr+1), etc.
It is a term often used with pointers and arrays, as you often take a base pointer value (or pointer to an array) and an offset to access the elements of the array.
Edit: Clicked submit to see 2 other answers appear - all slightly different, hope one of them explanis it for you:-)
Last edited on Jun 23, 2008 at 9:03pm
Jun 23, 2008 at 9:01pm
Hang on - is this actually 'extracting' anything?

ptr never changes so the return is the original value of 'a' with the byte changed.
Jun 23, 2008 at 9:04pm
@Zaita,

To your post as to what insight I am after, yes, you are right, I want to know what it is doing. I could put this program into a C++ debugger and see whats happening, but that would not make me understand anything, I need to understand conceptually.

The answers that I have come across on this forum have been helpful in making me see code such as this in a different way, it is perplexing to me and I feel that if I get to know more of whats happening under the hood, I could be a good C/C++ programmer someday.
Jun 23, 2008 at 9:06pm
@guestgulkan
No it isn't, I got it backwards. It's as Zaita said, changing one of the bytes to c.
ptr is added to the offset and the byte is modified in one go (line 13 on my post).
Last edited on Jun 23, 2008 at 9:08pm
Jun 23, 2008 at 9:07pm
Your just lucky I have spent the past few weeks doing byte/bit arithmetic and manipulation. I am writing a protocol handler for an embedded devices own protocol. Everything is in bytes, with strange data types I need to manipulate as bits.
Jun 23, 2008 at 9:11pm
@Zaita
Have you ever worked with the military datalink called Link 16 JTIDS? All the messages it exchanges are 9 bytes long and full of bitfields. One of the worst combinations imaginable. At times I could have happily shot the guy who designed it.

EDIT: My advice, if anyone offers you work on this (and just about every US ally uses it), run :)
Last edited on Jun 23, 2008 at 9:18pm
Jun 23, 2008 at 9:16pm
@bnbertha. God no lol, I try to avoid as much Govt stuff as possible. I was asked to write a new OS for a handheld laser GPS device that was being sold to USA Army. But they wanted to bring it into C#.NET and I didn't really want that to be my primary development language.

atm, I work for a science/research company. The work I am doing is a new interface into a logging device (climate logging, river water levels etc). Their protocol is pretty well defined, but they leave little bits out. This is in C#, but at byte/bit manipulation level i ts 90% the same as C++.
Last edited on Jun 23, 2008 at 9:16pm
Jun 23, 2008 at 9:22pm
I never have got why everyone is jumping on the C# wagon. The only thing I can see that it does is tie you to Microsoft :/
Jun 23, 2008 at 9:26pm
I was the same, I am not really fond of Microsoft and their business practices (or quality of products *cough*360 broke 2x now*cough*).

But, I don't mind C#. .NET is a bit of a PITA but C# is actually a fairly good language. They have a few strange things I miss but it's exceptional for creating web front-end applications. Especially with Web-Services.

J2EE offers the same functionality, but in a much larger and more complicated architecture. So .NET/C# is allowing grad/newbie programmers to create Web Front-end applications easily. Not to mention, schools/unis get Visual Studio pretty much free.

C++ needs a standard GUI Toolkit. One thats multi-platform. Personally, I use WxWidgets on any platform I am developing on, but C++ lacks this standard one that .NET/Java/Delphi offer. Thats a big feature of .NET/C#. Instant GUI

Edit: It also has garbage collection, so every edit can use 'new' without having to worry about 'delete'. Yet memory leaks still exist.. go figure :P
Last edited on Jun 23, 2008 at 9:27pm
Jun 23, 2008 at 9:34pm
You have to hand it to Microsoft, give Visual Studio, Office etc to the schools and Uni's and get all the students and grads used to using them. Then commercial sector goes with them cos there is a ready supply of users and developers.
Last edited on Jun 23, 2008 at 9:34pm
Jun 23, 2008 at 9:35pm
Yup. It's population brainwashing straight up. But thats how MS became who they are today. Taking this is thread a bit OT lol.

To sum C# up in 1 sentence. "it's like an easy version of C++, complete rip of Java made a bit simpler that most idiots (or monkeys) can code in". Nothing magic about it.
Pages: 12