So I want discussed with you casting and more casting allows you to do allows you to convert one type
into another.
So less true.
For example floats ABC eco zero dot now equals one dot five.
For example we now did integer A equals ABC we can convert this into back into an integer.
By doing this so bracket I anti bracket.
And now if we now output
the integer values for a and you compile AB
using JCC main dot c dash show Maine you will now see if we run Maine 1 you don't see 1 5.
We have basically casted the float from a float into an integer.
So that's how you can cast with primitive variables and you can cast to any of these types so you get
to char anything.
So you go char a equals char ABC.
That should work as well.
It does.
So now let's discuss how you can cast pointer types.
So I've written some code here to explain pointer casting.
Now you can see we create an integer called a.
We set it to hexadecimal value for 1 4 2 4 3 4 4.
Now these numbers are so important because if I show you the ascii table if we look a hexadecimal value
for one.
That's a hex decimal value for two That's B for three C footfall D.
Now as you know all integers can store four bytes by using hex decimal values.
This is the equivalent to us saying A is equal to ABC D because remember what I said.
That is old binary at the end of the day.
So you can do this now.
Here we create a char point that points to the address of a but we cast it to a char pointer.
So then what we can do is we can actually output these values as if they were Czech characters because
it's all just memory at the end of the day right.
If I did pointers point to zero here and I compile that
you c d if I did point a 1
U C C point to 2
you see B point a 3
you see a.
So we're actually outputting a b c and d just from this integer here and that is quite impressive because
in C you can cast a pointer to anywhere in memory that you have access to.
So by casting a pointer to our integer a as we cast it to char to a char pointer we can access it as
if it was something like this
we can literally cast it as if it was that because this will work as well you say.
So what I'm trying to say here is whether you use an integer or Char it's all stored the same in memory
so you X for 1 4 2 4 3 4 4 is stored just as if we made a char array of 4 bytes and we assigned it to
0 x 4 1 4 2 4 3 4 4 now during compile process you know the optimizer might do some clever things store
things in registers and so on.
But from a C perspective you can access things just like this.
And and it's completely legal and it is a common practice.
You can cast structures to areas in memory too for example fights find a structure here we'll call it
test or type test struct.
And here I have shot a choppy char c char D
I can then do something like this
s to a equals c you X for 3 s stop B equals zero x 4 for assault C equals zero x for five s top d equals
zero x for six I can now make a sharp pointer and I can pointed to the address of our structure casted
here to a char pointer and now I should be able to just do PCR zero and if I now run that we see C PCR
1
we c d PCR 2
we see E and that is really how it works you can cast any type literally with the point is you could
even cast a char pointer to a function if you really wanted to.
It is very powerful feature and is used throughout the entire C language.
So back to our original point example you may notice these bytes out put it in a in the wrong order.
Now there's a reason for that.
Processes have what is known as ending this.
And essentially you have big Indian and little Indian now Big Indian store it as we write it for 1 4
2 4 3 4 4 in hexadecimal you know the binary equivalence however little Indian is a bit different because
the bytes get swapped.
So this is stored a little differently now most processes are little Indian so you should definitely
read about a little Indian and big Indian to understand that more further.
But essentially the the bytes can get swapped out for little Indian machines whereas big Indian machines
it is how you write it.
And this is also pretty important to note because if you have a developing network applications then
you ideally will need to convert the value here to a big Indian value so that when you send it down
that the network the the guy on the other end can read this big Indian because if you have two machines
one little Indian and one on big Indian and they're communicating over the network they won't work as
expected unless they both follow the same Indian ness.
So essentially if a little Indian machine receives a big Indian number it will convert it to little
Indian when it receives it and big Indian machines will do nothing because they know that this is big
Indian the network standard is to send as big Indian to send in just as big Indian.
Now this sort of flipping stuff will only ever happen with primitive types and when you assign them
like this.
So when it comes to things like child chars light like in the structure we had where I had a a structure
with about four full charge you'll notice that was an order.
That's because chores were only one by if you use short or higher then you need to take into consideration
that lending machines will tend to flip the bytes but definitely read about little lending in big Indian
as they are not part of this C costs.