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. -
unsigned char t[2];
-
unsigned short *p;
-
t[0]=1;
-
t[1]=2;
-
*p=(unsigned char *) *t;
-
how will be the value stored in the memory while reading if starting address of p is 0x 04000000
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. -
void main(){
-
int a[] = {1,2,3,4,5};
-
printf("%d\n",a);
-
}
-
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! -
void main(){
-
int a[] = {1,2,3,4,5};
-
printf("%d\n",*a);
-
}
-
The value is 1, so this is the same as -
void main(){
-
int a[] = {1,2,3,4,5};
-
printf("%d\n",a[0]);
-
}
-
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: -
void main(){
-
int a[] = {1,2,3,4,5};
-
int *b;
-
b = a;
-
printf("%d\n",b);
-
printf("%d\n",*b);
-
}
-
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. -
void main(){
-
int a[] = {1,2,3,4,5};
-
printf("%d\n",(char*)*a);
-
}
-
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... -
void main(){
-
int a[] = {1,2,3,4,5};
-
char *b = (char*)*a;
-
printf("%d\n",(char*)*a);
-
printf("%d\n",*b);
-
}
-
Did it segfault? I think this part of the memory would be used by the bootloader.
3 9773
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.
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. -
void main(){
-
int a[] = {1,2,3,4,5};
-
printf("%d\n",a);
-
}
-
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! -
void main(){
-
int a[] = {1,2,3,4,5};
-
printf("%d\n",*a);
-
}
-
The value is 1, so this is the same as -
void main(){
-
int a[] = {1,2,3,4,5};
-
printf("%d\n",a[0]);
-
}
-
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: -
void main(){
-
int a[] = {1,2,3,4,5};
-
int *b;
-
b = a;
-
printf("%d\n",b);
-
printf("%d\n",*b);
-
}
-
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. -
void main(){
-
int a[] = {1,2,3,4,5};
-
printf("%d\n",(char*)*a);
-
}
-
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... -
void main(){
-
int a[] = {1,2,3,4,5};
-
char *b = (char*)*a;
-
printf("%d\n",(char*)*a);
-
printf("%d\n",*b);
-
}
-
Did it segfault? I think this part of the memory would be used by the bootloader.
thanks now i got something understood
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Xiangliang Meng |
last post by:
Hi.
void setValue(int n)
{
int size = getValueLength();
int buffer_p;
if (buffer_p)
{
....
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| |