storing 16 bit variable to dereferenced variable writes 32 bitsHow do you set, clear, and toggle a single bit?What are the differences between a pointer variable and a reference variable in C++?How would one write object-oriented code in C?Is the practice of returning a C++ reference variable evil?How do I pass a variable by reference?How do I use extern to share variables between source files?What does “dereferencing” a pointer mean?Extracting bits with a single multiplicationint to a uint8_t in HEX, for example 0x07 stored in a uint8_t. In CShare typedef struct array used in file among other files
Why is there not an optimal solution for a MIP?
What is the need of methods like GET and POST in the HTTP protocol?
A drug that allows people to survive on less food
Resolving moral conflict
Performance for simple code that converts a RGB tuple to hex string
Do all creatures have souls?
Safely hang a mirror that does not have hooks
Late 1970's and 6502 chip facilities for operating systems
Did Apollo carry and use WD40?
A high quality contribution but an annoying error is present in my published article
Does wetting a beer glass change the foam characteristics?
How do I deal with too many NPCs in my campaign?
Should I complain to HR about being mocked for request I made
In a folk jam session, when asked which key my non-transposing chromatic instrument (like a violin) is in, what do I answer?
Writing a letter of recommendation for a mediocre student
Do we know the situation in Britain before Sealion (summer 1940)?
Hilbert's hotel, why can't I repeat it infinitely many times?
How to conditionally load a package only if shell-escape (write18) is passed
How to manage expenditure when billing cycles and paycheck cycles are not aligned?
Conditionally execute a command if a specific package is loaded
Worms crawling under skin
My 15 year old son is gay. How do I express my feelings about this?
Why does NASA publish all the results/data it gets?
Can this word order be rearranged?
storing 16 bit variable to dereferenced variable writes 32 bits
How do you set, clear, and toggle a single bit?What are the differences between a pointer variable and a reference variable in C++?How would one write object-oriented code in C?Is the practice of returning a C++ reference variable evil?How do I pass a variable by reference?How do I use extern to share variables between source files?What does “dereferencing” a pointer mean?Extracting bits with a single multiplicationint to a uint8_t in HEX, for example 0x07 stored in a uint8_t. In CShare typedef struct array used in file among other files
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Consider the following:
// main.h
struct
uint16_t aChargeOption0;
uint16_t aChargeOption1;
oBattChargerInfo;
typedef struct CHGRRM
uint16_t nRegIndex;
uint8_t nDataType;
uint32_t nDataWidth;
uint32_t nRegAddress;
bool IsWritable;
bool HasBits;
uint32_t nBitStoreStart;
uint32_t nBitStoreEnd;
int *ptrToData;
chargerRegMap_t;
extern chargerRegMap_t charger_reg_map[];
// main.c
chargerRegMap_t charger_reg_map[] =
&oBattChargerInfo.aChargeOption0 ,
&oBattChargerInfo.aChargeOption1 ,
;
// code to store a variable to the de-referenced variable
uint16_t aFinalBuff=0x00;
aFinalBuff=buff[1]<<8; // buff[0] and buff[1] is uint8
aFinalBuff=aFinalBuff+buff[0];
*charger_reg_map[nRegIndex].ptrToData=aFinalBuff;
When I store the first variable (charger_reg_map[0].ptrToData
which in the first case is oBattChargerInfo.aChargeOption0
) the 16 bit variable overwrites the adjacent variable oBattChargerInfo.aChargeOption1
. Other than setting each variable in my oBattChargerInfo
structure to 32 bits each, is there another solution? It seems strange that a dereferenced variable would work this way.
I tried *charger_reg_map[nRegIndex].ptrToData=(uint16_t)aFinalBuff;
to make clear my intention. Didn't matter.
What am I doing wrong here?
c reference
add a comment
|
Consider the following:
// main.h
struct
uint16_t aChargeOption0;
uint16_t aChargeOption1;
oBattChargerInfo;
typedef struct CHGRRM
uint16_t nRegIndex;
uint8_t nDataType;
uint32_t nDataWidth;
uint32_t nRegAddress;
bool IsWritable;
bool HasBits;
uint32_t nBitStoreStart;
uint32_t nBitStoreEnd;
int *ptrToData;
chargerRegMap_t;
extern chargerRegMap_t charger_reg_map[];
// main.c
chargerRegMap_t charger_reg_map[] =
&oBattChargerInfo.aChargeOption0 ,
&oBattChargerInfo.aChargeOption1 ,
;
// code to store a variable to the de-referenced variable
uint16_t aFinalBuff=0x00;
aFinalBuff=buff[1]<<8; // buff[0] and buff[1] is uint8
aFinalBuff=aFinalBuff+buff[0];
*charger_reg_map[nRegIndex].ptrToData=aFinalBuff;
When I store the first variable (charger_reg_map[0].ptrToData
which in the first case is oBattChargerInfo.aChargeOption0
) the 16 bit variable overwrites the adjacent variable oBattChargerInfo.aChargeOption1
. Other than setting each variable in my oBattChargerInfo
structure to 32 bits each, is there another solution? It seems strange that a dereferenced variable would work this way.
I tried *charger_reg_map[nRegIndex].ptrToData=(uint16_t)aFinalBuff;
to make clear my intention. Didn't matter.
What am I doing wrong here?
c reference
*charger_reg_map[nRegIndex].ptrToData=aFinalBuff;
<- here you dereference the pointer stored in ptrToData. Is that what you really want? Is it initialized correctly? You do not set the struct memberptrToData
with this statement!
– Ctx
Mar 28 at 15:55
ptrToData
is anint*
- that is, most likely a pointer to a 32-bit integer. You can't write anything of a different size from anint
through anint*
.
– molbdnilo
Mar 28 at 16:01
I want the structure member ptrToData to hold a reference to the variable which I may then access and assign a value to in later code. Here's a simpler way to express the problem: ```` uint16_t aVars[2]; aVars[0]=0xff; aVars[1]=0xff; int *ptrToData; ptrToData=&aVars[0]; *ptrToData=0xAA; ```` Is there another method I may use to reference variables programmatically? In the past, I've never had an issue with this because they were all 32 bit.
– Mark Richards
Mar 28 at 16:31
1
Why not just use a pointer of the appropriate type?
– molbdnilo
Mar 28 at 18:34
@molbdnilo My data structure has variables of uint8, uint16, uint32, ect. For now I've changed all my structure vars to uint32, until a better solution can be found.
– Mark Richards
Mar 28 at 20:25
add a comment
|
Consider the following:
// main.h
struct
uint16_t aChargeOption0;
uint16_t aChargeOption1;
oBattChargerInfo;
typedef struct CHGRRM
uint16_t nRegIndex;
uint8_t nDataType;
uint32_t nDataWidth;
uint32_t nRegAddress;
bool IsWritable;
bool HasBits;
uint32_t nBitStoreStart;
uint32_t nBitStoreEnd;
int *ptrToData;
chargerRegMap_t;
extern chargerRegMap_t charger_reg_map[];
// main.c
chargerRegMap_t charger_reg_map[] =
&oBattChargerInfo.aChargeOption0 ,
&oBattChargerInfo.aChargeOption1 ,
;
// code to store a variable to the de-referenced variable
uint16_t aFinalBuff=0x00;
aFinalBuff=buff[1]<<8; // buff[0] and buff[1] is uint8
aFinalBuff=aFinalBuff+buff[0];
*charger_reg_map[nRegIndex].ptrToData=aFinalBuff;
When I store the first variable (charger_reg_map[0].ptrToData
which in the first case is oBattChargerInfo.aChargeOption0
) the 16 bit variable overwrites the adjacent variable oBattChargerInfo.aChargeOption1
. Other than setting each variable in my oBattChargerInfo
structure to 32 bits each, is there another solution? It seems strange that a dereferenced variable would work this way.
I tried *charger_reg_map[nRegIndex].ptrToData=(uint16_t)aFinalBuff;
to make clear my intention. Didn't matter.
What am I doing wrong here?
c reference
Consider the following:
// main.h
struct
uint16_t aChargeOption0;
uint16_t aChargeOption1;
oBattChargerInfo;
typedef struct CHGRRM
uint16_t nRegIndex;
uint8_t nDataType;
uint32_t nDataWidth;
uint32_t nRegAddress;
bool IsWritable;
bool HasBits;
uint32_t nBitStoreStart;
uint32_t nBitStoreEnd;
int *ptrToData;
chargerRegMap_t;
extern chargerRegMap_t charger_reg_map[];
// main.c
chargerRegMap_t charger_reg_map[] =
&oBattChargerInfo.aChargeOption0 ,
&oBattChargerInfo.aChargeOption1 ,
;
// code to store a variable to the de-referenced variable
uint16_t aFinalBuff=0x00;
aFinalBuff=buff[1]<<8; // buff[0] and buff[1] is uint8
aFinalBuff=aFinalBuff+buff[0];
*charger_reg_map[nRegIndex].ptrToData=aFinalBuff;
When I store the first variable (charger_reg_map[0].ptrToData
which in the first case is oBattChargerInfo.aChargeOption0
) the 16 bit variable overwrites the adjacent variable oBattChargerInfo.aChargeOption1
. Other than setting each variable in my oBattChargerInfo
structure to 32 bits each, is there another solution? It seems strange that a dereferenced variable would work this way.
I tried *charger_reg_map[nRegIndex].ptrToData=(uint16_t)aFinalBuff;
to make clear my intention. Didn't matter.
What am I doing wrong here?
c reference
c reference
edited Mar 28 at 15:52
bolov
38.1k11 gold badges86 silver badges151 bronze badges
38.1k11 gold badges86 silver badges151 bronze badges
asked Mar 28 at 15:50
Mark RichardsMark Richards
1021 silver badge10 bronze badges
1021 silver badge10 bronze badges
*charger_reg_map[nRegIndex].ptrToData=aFinalBuff;
<- here you dereference the pointer stored in ptrToData. Is that what you really want? Is it initialized correctly? You do not set the struct memberptrToData
with this statement!
– Ctx
Mar 28 at 15:55
ptrToData
is anint*
- that is, most likely a pointer to a 32-bit integer. You can't write anything of a different size from anint
through anint*
.
– molbdnilo
Mar 28 at 16:01
I want the structure member ptrToData to hold a reference to the variable which I may then access and assign a value to in later code. Here's a simpler way to express the problem: ```` uint16_t aVars[2]; aVars[0]=0xff; aVars[1]=0xff; int *ptrToData; ptrToData=&aVars[0]; *ptrToData=0xAA; ```` Is there another method I may use to reference variables programmatically? In the past, I've never had an issue with this because they were all 32 bit.
– Mark Richards
Mar 28 at 16:31
1
Why not just use a pointer of the appropriate type?
– molbdnilo
Mar 28 at 18:34
@molbdnilo My data structure has variables of uint8, uint16, uint32, ect. For now I've changed all my structure vars to uint32, until a better solution can be found.
– Mark Richards
Mar 28 at 20:25
add a comment
|
*charger_reg_map[nRegIndex].ptrToData=aFinalBuff;
<- here you dereference the pointer stored in ptrToData. Is that what you really want? Is it initialized correctly? You do not set the struct memberptrToData
with this statement!
– Ctx
Mar 28 at 15:55
ptrToData
is anint*
- that is, most likely a pointer to a 32-bit integer. You can't write anything of a different size from anint
through anint*
.
– molbdnilo
Mar 28 at 16:01
I want the structure member ptrToData to hold a reference to the variable which I may then access and assign a value to in later code. Here's a simpler way to express the problem: ```` uint16_t aVars[2]; aVars[0]=0xff; aVars[1]=0xff; int *ptrToData; ptrToData=&aVars[0]; *ptrToData=0xAA; ```` Is there another method I may use to reference variables programmatically? In the past, I've never had an issue with this because they were all 32 bit.
– Mark Richards
Mar 28 at 16:31
1
Why not just use a pointer of the appropriate type?
– molbdnilo
Mar 28 at 18:34
@molbdnilo My data structure has variables of uint8, uint16, uint32, ect. For now I've changed all my structure vars to uint32, until a better solution can be found.
– Mark Richards
Mar 28 at 20:25
*charger_reg_map[nRegIndex].ptrToData=aFinalBuff;
<- here you dereference the pointer stored in ptrToData. Is that what you really want? Is it initialized correctly? You do not set the struct member ptrToData
with this statement!– Ctx
Mar 28 at 15:55
*charger_reg_map[nRegIndex].ptrToData=aFinalBuff;
<- here you dereference the pointer stored in ptrToData. Is that what you really want? Is it initialized correctly? You do not set the struct member ptrToData
with this statement!– Ctx
Mar 28 at 15:55
ptrToData
is an int*
- that is, most likely a pointer to a 32-bit integer. You can't write anything of a different size from an int
through an int*
.– molbdnilo
Mar 28 at 16:01
ptrToData
is an int*
- that is, most likely a pointer to a 32-bit integer. You can't write anything of a different size from an int
through an int*
.– molbdnilo
Mar 28 at 16:01
I want the structure member ptrToData to hold a reference to the variable which I may then access and assign a value to in later code. Here's a simpler way to express the problem: ```` uint16_t aVars[2]; aVars[0]=0xff; aVars[1]=0xff; int *ptrToData; ptrToData=&aVars[0]; *ptrToData=0xAA; ```` Is there another method I may use to reference variables programmatically? In the past, I've never had an issue with this because they were all 32 bit.
– Mark Richards
Mar 28 at 16:31
I want the structure member ptrToData to hold a reference to the variable which I may then access and assign a value to in later code. Here's a simpler way to express the problem: ```` uint16_t aVars[2]; aVars[0]=0xff; aVars[1]=0xff; int *ptrToData; ptrToData=&aVars[0]; *ptrToData=0xAA; ```` Is there another method I may use to reference variables programmatically? In the past, I've never had an issue with this because they were all 32 bit.
– Mark Richards
Mar 28 at 16:31
1
1
Why not just use a pointer of the appropriate type?
– molbdnilo
Mar 28 at 18:34
Why not just use a pointer of the appropriate type?
– molbdnilo
Mar 28 at 18:34
@molbdnilo My data structure has variables of uint8, uint16, uint32, ect. For now I've changed all my structure vars to uint32, until a better solution can be found.
– Mark Richards
Mar 28 at 20:25
@molbdnilo My data structure has variables of uint8, uint16, uint32, ect. For now I've changed all my structure vars to uint32, until a better solution can be found.
– Mark Richards
Mar 28 at 20:25
add a comment
|
1 Answer
1
active
oldest
votes
If you want differently-sized variables, there is no pointer type you can use to write to all of them.
The design is a bit brittle, since you would need to know which member your pointer is pointing to, or at least its type.
I think a better option would be to do the work with memcpy
through a function, and add a 'size' member to chargerRegMap_t
for sanity checking.
Something like
void write_data(chargerRegMap_t* map, void* data, size_t size)
assert(size == map->data_size);
memcpy(map->ptrToData, data, size);
/* ... */
uint16_t aFinalBuff = 0x00;
aFinalBuff = buff[1] << 8;
aFinalBuff = aFinalBuff + buff[0];
write_data(charger_reg_map + nRegIndex, &aFinalBuff, sizeof(aFinalBuff));
I would probably add a macro to that as well, to eliminate sizeof
typos and make it less tedious:
#define WRITE_DATA(map, data) write_data(map, &data, sizeof(data))
/* ... */
WRITE_DATA(charger_reg_map + nRegIndex, aFinalBuff);
add a comment
|
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55401872%2fstoring-16-bit-variable-to-dereferenced-variable-writes-32-bits%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you want differently-sized variables, there is no pointer type you can use to write to all of them.
The design is a bit brittle, since you would need to know which member your pointer is pointing to, or at least its type.
I think a better option would be to do the work with memcpy
through a function, and add a 'size' member to chargerRegMap_t
for sanity checking.
Something like
void write_data(chargerRegMap_t* map, void* data, size_t size)
assert(size == map->data_size);
memcpy(map->ptrToData, data, size);
/* ... */
uint16_t aFinalBuff = 0x00;
aFinalBuff = buff[1] << 8;
aFinalBuff = aFinalBuff + buff[0];
write_data(charger_reg_map + nRegIndex, &aFinalBuff, sizeof(aFinalBuff));
I would probably add a macro to that as well, to eliminate sizeof
typos and make it less tedious:
#define WRITE_DATA(map, data) write_data(map, &data, sizeof(data))
/* ... */
WRITE_DATA(charger_reg_map + nRegIndex, aFinalBuff);
add a comment
|
If you want differently-sized variables, there is no pointer type you can use to write to all of them.
The design is a bit brittle, since you would need to know which member your pointer is pointing to, or at least its type.
I think a better option would be to do the work with memcpy
through a function, and add a 'size' member to chargerRegMap_t
for sanity checking.
Something like
void write_data(chargerRegMap_t* map, void* data, size_t size)
assert(size == map->data_size);
memcpy(map->ptrToData, data, size);
/* ... */
uint16_t aFinalBuff = 0x00;
aFinalBuff = buff[1] << 8;
aFinalBuff = aFinalBuff + buff[0];
write_data(charger_reg_map + nRegIndex, &aFinalBuff, sizeof(aFinalBuff));
I would probably add a macro to that as well, to eliminate sizeof
typos and make it less tedious:
#define WRITE_DATA(map, data) write_data(map, &data, sizeof(data))
/* ... */
WRITE_DATA(charger_reg_map + nRegIndex, aFinalBuff);
add a comment
|
If you want differently-sized variables, there is no pointer type you can use to write to all of them.
The design is a bit brittle, since you would need to know which member your pointer is pointing to, or at least its type.
I think a better option would be to do the work with memcpy
through a function, and add a 'size' member to chargerRegMap_t
for sanity checking.
Something like
void write_data(chargerRegMap_t* map, void* data, size_t size)
assert(size == map->data_size);
memcpy(map->ptrToData, data, size);
/* ... */
uint16_t aFinalBuff = 0x00;
aFinalBuff = buff[1] << 8;
aFinalBuff = aFinalBuff + buff[0];
write_data(charger_reg_map + nRegIndex, &aFinalBuff, sizeof(aFinalBuff));
I would probably add a macro to that as well, to eliminate sizeof
typos and make it less tedious:
#define WRITE_DATA(map, data) write_data(map, &data, sizeof(data))
/* ... */
WRITE_DATA(charger_reg_map + nRegIndex, aFinalBuff);
If you want differently-sized variables, there is no pointer type you can use to write to all of them.
The design is a bit brittle, since you would need to know which member your pointer is pointing to, or at least its type.
I think a better option would be to do the work with memcpy
through a function, and add a 'size' member to chargerRegMap_t
for sanity checking.
Something like
void write_data(chargerRegMap_t* map, void* data, size_t size)
assert(size == map->data_size);
memcpy(map->ptrToData, data, size);
/* ... */
uint16_t aFinalBuff = 0x00;
aFinalBuff = buff[1] << 8;
aFinalBuff = aFinalBuff + buff[0];
write_data(charger_reg_map + nRegIndex, &aFinalBuff, sizeof(aFinalBuff));
I would probably add a macro to that as well, to eliminate sizeof
typos and make it less tedious:
#define WRITE_DATA(map, data) write_data(map, &data, sizeof(data))
/* ... */
WRITE_DATA(charger_reg_map + nRegIndex, aFinalBuff);
answered Mar 28 at 21:19
molbdnilomolbdnilo
45.4k3 gold badges24 silver badges59 bronze badges
45.4k3 gold badges24 silver badges59 bronze badges
add a comment
|
add a comment
|
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55401872%2fstoring-16-bit-variable-to-dereferenced-variable-writes-32-bits%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
*charger_reg_map[nRegIndex].ptrToData=aFinalBuff;
<- here you dereference the pointer stored in ptrToData. Is that what you really want? Is it initialized correctly? You do not set the struct memberptrToData
with this statement!– Ctx
Mar 28 at 15:55
ptrToData
is anint*
- that is, most likely a pointer to a 32-bit integer. You can't write anything of a different size from anint
through anint*
.– molbdnilo
Mar 28 at 16:01
I want the structure member ptrToData to hold a reference to the variable which I may then access and assign a value to in later code. Here's a simpler way to express the problem: ```` uint16_t aVars[2]; aVars[0]=0xff; aVars[1]=0xff; int *ptrToData; ptrToData=&aVars[0]; *ptrToData=0xAA; ```` Is there another method I may use to reference variables programmatically? In the past, I've never had an issue with this because they were all 32 bit.
– Mark Richards
Mar 28 at 16:31
1
Why not just use a pointer of the appropriate type?
– molbdnilo
Mar 28 at 18:34
@molbdnilo My data structure has variables of uint8, uint16, uint32, ect. For now I've changed all my structure vars to uint32, until a better solution can be found.
– Mark Richards
Mar 28 at 20:25