473,545 Members | 1,288 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What will happen when typecasting 8 bit pointer to 16 bit pointer?

4 New Member
Hi all,
I have one unsigned char array[2] of 8 bit.it contains value of same type.i tried to assign to unsigned short(16 bits) pointer by type casting.now how will be the value stored in a pointer.please refer this example code.
Expand|Select|Wrap|Line Numbers
  1. unsigned char t[2];
  2. unsigned short *p;
  3. t[0]=1;
  4. t[1]=2;
  5. *p=(unsigned char *) *t;
  6.  
how will be the value stored in the memory while reading if starting address of p is 0x 04000000
May 27 '14 #1
3 9820
weaknessforcats
9,208 Recognized Expert Moderator Expert
Well, t is a char* so *t is a char. The typecast tells the compiler to consider it to be a pointer to an unsigned char. The compiler knows this is phony but does what you say because you must know something the compiler doesn't.

If the value in the *t is 25 then that's the value of your unsigned char*.

After a cast you, and not the compiler , is responsible for correct values.
May 27 '14 #2
kudos
127 Recognized Expert New Member
Pointers, it seems to be surrounded with a certain mystique.

First, a pointer is something that points to the memory in your computer. The memory is an array.

for instance;

int a[] = {1,2,3,4,5};

An array right, this array is placed somewhere in memory.

Expand|Select|Wrap|Line Numbers
  1. void main(){
  2. int a[] = {1,2,3,4,5};
  3. printf("%d\n",a);
  4. }
  5.  
You probably get some warnings, and the output is a weird number. The value is the location in the memory where the array a is placed.

Now, lets prove that this is a pointer!

Expand|Select|Wrap|Line Numbers
  1. void main(){
  2. int a[] = {1,2,3,4,5};
  3. printf("%d\n",*a);
  4. }
  5.  
The value is 1, so this is the same as

Expand|Select|Wrap|Line Numbers
  1. void main(){
  2. int a[] = {1,2,3,4,5};
  3. printf("%d\n",a[0]);
  4. }
  5.  
Now, we need to be a bit careful...becau se there is two things here! The address of the element (i.e., the weird value) and the content of that address which is 1. One more example:

Expand|Select|Wrap|Line Numbers
  1. void main(){
  2. int a[] = {1,2,3,4,5};
  3. int *b;
  4. b = a;
  5. printf("%d\n",b);
  6. printf("%d\n",*b);
  7. }
  8.  
Here we see the difference between the address of the pointer and the content of that pointer. Now....here is where you want to be careful. Typecasting the value of the pointer, for instance 1 fits inside an 8 bit value.

Expand|Select|Wrap|Line Numbers
  1. void main(){
  2. int a[] = {1,2,3,4,5};
  3. printf("%d\n",(char*)*a);
  4. }
  5.  
Here we tell, lets turn the value 1 into an 8 bit pointer. But, we try ask, what address does this pointer point to...well, it points to the memory address 1. We do not try to actually read what is placed at memory location 1. Ofcourse, we could try...

Expand|Select|Wrap|Line Numbers
  1. void main(){
  2. int a[] = {1,2,3,4,5};
  3. char *b = (char*)*a;
  4. printf("%d\n",(char*)*a);
  5. printf("%d\n",*b);
  6. }
  7.  
Did it segfault? I think this part of the memory would be used by the bootloader.
May 29 '14 #3
vigneshmck
4 New Member
thanks now i got something understood
Jun 2 '14 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

18
2919
by: Xiangliang Meng | last post by:
Hi. void setValue(int n) { int size = getValueLength(); int buffer_p; if (buffer_p) { ....
0
7457
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7391
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...
1
7410
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7746
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...
0
5962
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5320
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...
1
1869
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
1
1010
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
693
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...

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.