H
Hemant Mohan
Consider the following program snipet:
<snip>
typedef struct
{
unsigned char a ;
unsigned char b ;
unsigned char c ;
unsigned char* d_ptr; /* Pointer to some data array */
} str_t ;
int main()
{
unsigned char arr[12] =
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x0A, 0x0B };
str_t *my_str = NULL;
my_str = (str_t*)(&arr[0]) ; /* first assignment */
my_str->d_ptr = &(arr[3]) ; /*second assignment*/
return 0;
}
</snip>
On execution of this program I notice that the "second assignment"
modifies the rvalue that is arr at indexes 4,5,6,7 to address of
my_str->d_ptr.
The d_ptr goes to index 4 instead of index 3 probabaly due to
allignment ( padding ) issues.
However, I am unable to explain why the rvalue in the array gets
modified, as normally assignments only modify the lvaue. Also how do I
overcome this problem.
My initial guess is that my_str->d_ptr is a pointer to a pointer and
due to this the rvalue also gets modified.
Thanks in advance
Hemant
<snip>
typedef struct
{
unsigned char a ;
unsigned char b ;
unsigned char c ;
unsigned char* d_ptr; /* Pointer to some data array */
} str_t ;
int main()
{
unsigned char arr[12] =
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x0A, 0x0B };
str_t *my_str = NULL;
my_str = (str_t*)(&arr[0]) ; /* first assignment */
my_str->d_ptr = &(arr[3]) ; /*second assignment*/
return 0;
}
</snip>
On execution of this program I notice that the "second assignment"
modifies the rvalue that is arr at indexes 4,5,6,7 to address of
my_str->d_ptr.
The d_ptr goes to index 4 instead of index 3 probabaly due to
allignment ( padding ) issues.
However, I am unable to explain why the rvalue in the array gets
modified, as normally assignments only modify the lvaue. Also how do I
overcome this problem.
My initial guess is that my_str->d_ptr is a pointer to a pointer and
due to this the rvalue also gets modified.
Thanks in advance
Hemant