#include <stdio.h>
#include <limits.h>
#include <string.h>
#include <stdlib.h>
typedef struct
{
unsigned char a[3];
unsigned short data[3];
unsigned int d1;
unsigned int d2;
} abc;
void f1(abc *ptr)
{
printf("values are :0x%x \t0x%x \t0%x",ptr->data[0],ptr->data[1],ptr->data[2]);
//Hex map of ptr is given below (taken by gdb --> x/20b ptr )
//ptr-->0x01 0x00 0x00 0x00 0x44 0x29 0xff 0xff
// 0xff 0xff
}
void f()
{
abc req;
req.a[0]=1;
req.a[1]=0;
req.a[2]=0;
req.data[0]=0x2944;
req.data[1]=0xffff;
req.data[2]=0xffff;
req.d1= 0xffff;
req.d2= 0xffff;
f1(&req);
//Hex map of req is given below (taken by gdb --> x/20b req )
// req--> 0x01 0x00 0x00 0x00 0x44 0x29 0xff 0xff
// 0xff 0xff
}
int main()
{
f();
return 0;
}
I am working on the code sample given above . filling values in one struct in 1 function and passing struct to other function. But values of struct are changed in the caller function. When I checked with gdb, struct byte representation is same. But some how values are changed. Can any one explain what happened and how to overcome it gcc version is 4.9.3 and gdb version is 7.7.1 and OS is ubuntu 14.
Below is the output from gdb.
req values in f() -->
(gdb) p/x req
$1 = {
a = {0x1, 0x0, 0x0},
data = {0x2944, 0xffff, 0xffff},
d1 = 0xffff,
d2 = 0xffff
(gdb) x/20b &req
0xffffcb80: 0x01 0x00 0x00 0x00 0x44 0x29 0xff 0xff
ptr values in f1()---->
(gdb) p/x *ptr
$1 = {
a = {0x1, 0x0, 0x0},
data = {0x4400, 0xff29, 0xffff},
(gdb) x/20b ptr
0xffffcb80: 0x01 0x00 0x00 0x00 0x44 0x29 0xff 0xff