473,382 Members | 1,526 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,382 software developers and data experts.

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

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

✓ answered by kudos

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...because 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.

3 9773
weaknessforcats
9,208 Expert Mod 8TB
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 Expert 100+
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...because 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
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
by: Xiangliang Meng | last post by:
Hi. void setValue(int n) { int size = getValueLength(); int buffer_p; if (buffer_p) { ....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.