Home > Software design >  What does this C Pointer Code Produce? [ulong pointer -> uint]
What does this C Pointer Code Produce? [ulong pointer -> uint]

Time:10-08

Hey Peeps,

Am currently working on porting some old C Library to C# but have a bit of trouble understanding a certain piece of Code containing Pointers.
Am not the best when it comes to C so i might be lacking some understanding.

Heres a simplified version of what im Looking at:

unsigned int block[2];

// --- inside a function

unsigned int *block;       // only got access to this pointer not the array
unsigned long left, right;

// some math


*block   = right;  // these lines are the important bit i dont quite get
*block = left;

now...

What I think i got so far is:

  • The first line...
    • dereferences the pointer
    • sets its value to right
    • steps the pointer forward by 1
  • And the second line...
    • dereferences the pointer
    • sets its value to left

Now what i have trouble wrapping my head around is how the end result (blocks[]) looks.
(Sadly cant just debug it and take a peek bc I only have dont really know how id do that with a lib binary...)
Would be fairly simple if left and right were uints aswell but they are both ulongs so theres probably some sort of overwriting going on right?

Am a little confused / lost on this one... Maybe some of you with some better C knowledge can help me out ^^

CodePudding user response:

This is basically doing:

block[0] = (unsigned int) right;
block[1] = (unsigned int) left;

And casting unsigned long to unsigned int simply discards the excess high order bits.

  • Related