Need to read value of array elements from one function inside another function using pointerAre there any downsides to passing structs by value in C, rather than passing a pointer?Handling xor with different key sizes and endianessC - Read in file according to a formatUnion struct: printing struct member of type uint32_t skips two bytes and prints wrong valueHow to get a pointer on the return value of a function?Typecasting a pointer to change its size (in bytes)What is the full function of TCPIP_TCP_ArrayGet of Harmony Configurator?Passing pointer/array to main() from a functionPassing and reading a C triple-pointer against a native function with refPortable reinterpretation of uint8_t as int8_t and forcing two's complement

Gofer work in exchange for LoR

What ways are there to share spells between characters, besides a Ring of Spell Storing?

I was dismissed as a candidate for an abroad company after disclosing my disability

Do I need to start off my book by describing the character's "normal world"?

Has there ever been a truly bilingual country prior to the contemporary period?

Has the speed of light ever been measured in vacuum?

How to train a replacement without them knowing?

Is there a way, other than having a Diviner friend, for a player to avoid rolling Initiative at the start of a combat?

Did Michelle Obama have a staff of 23; and Melania have a staff of 4?

Can I use my OWN published papers' images in my thesis without Copyright infringment

How would armour (and combat) change if the fighter didn't need to actually wear it?

Why is su world executable?

Knights and Knaves on a (Not So) Deserted Island

Resource is refusing to do a handover before leaving

Java methods to add and authenticate users in MySQL

Are there any cons in using rounded corners for bar graphs?

The space of cusp forms for GL_2 over F_q(T)

What allows us to use imaginary numbers?

How to mock ApexTestQueueItem, AsyncApexJob, and ApexTestResult for test coverage?

Will some rockets really collapse under their own weight?

Duplicate and slide edge (rip from boundary)

Change the default Bookmarks Folder In Firefox

What does 〇〇〇〇 mean when combined with おじさん?

Build a mob of suspiciously happy lenny faces ( ͡° ͜ʖ ͡°)



Need to read value of array elements from one function inside another function using pointer


Are there any downsides to passing structs by value in C, rather than passing a pointer?Handling xor with different key sizes and endianessC - Read in file according to a formatUnion struct: printing struct member of type uint32_t skips two bytes and prints wrong valueHow to get a pointer on the return value of a function?Typecasting a pointer to change its size (in bytes)What is the full function of TCPIP_TCP_ArrayGet of Harmony Configurator?Passing pointer/array to main() from a functionPassing and reading a C triple-pointer against a native function with refPortable reinterpretation of uint8_t as int8_t and forcing two's complement






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I am developing a code in which I need to read value from one function ("Read()") into another function ("compare") using pointer. Need to access values of "buffer" present in function "Read()". I have tried below approach, I am new with pointers implementation so not able to try much. "buffer" will have 9 elements which I have copied in "adata" in "Read()" function, need to have that data in "Compare" function.
Below is my code snippet:



 int16_t *Read (uint8_t *buffer, const uint16_t length)

static uint32_t totalBytes = 0;
static uint32_t respNo = 0;
int i;

GPIO_ClearValue(PMAP_GPIO_PORT_DIR_RS485, PMAP_GPIO_PIN_DIR_RS485);

UartRxFlush(UARW_UART_INDEX_RS485); //this function needs to be executed after the GPIO_ClearValue in order to remove the "zero" value from the buffer
respNo++;

int counter = 0; // counts n. valid bytes put in slave response buffer[]
do

OSTimeDly(2);

int8_t newBytesRcv = UartReceive(UARW_UART_INDEX_RS485,
(uint8_t*)&(buffer[counter]), length-counter);
totalBytes += newBytesRcv;
counter = totalBytes;

while (counter < length);

totalBytes = 0;
printf("n");
printf("Byte received........");
int16_t adata[9] = 0x09,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30;

for (i=0; i<9;i++)

printf("%d ",buffer[i]);
adata[i] = buffer[i];

printf("n");



int16_t Compare(uint8_t * message, uint16_t len)

rs485_message_t rs485Msg;
int StP15Vmsg[9] = 0x09,0x31,0x33,0x30,0x30,0x30,0x30,0x30,0x34;
int adata[9] = 0x09,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30;
uint8_t buffer[9];
const uint8_t length;
for (int j=0; j<=9; j++)

adata[j] = *Read(buffer, j);

if(compareArray(FWmsg,arduinodata,7)==0)


char res;
uint8_t add, fwcommand, fwaction;
uint16_t fwvalue;
rs485_message_t rs485Msg;
proto485ComposeMsg(&rs485Msg, &add, &fwcommand, &fwaction, &fwvalue);
res = GetABFWversion(&add, &fwcommand, &fwaction, &fwvalue);




Please guide....



Thank you










share|improve this question





















  • 1





    In your Compare function you go out of bounds of the adata array.

    – Some programmer dude
    Mar 27 at 11:14






  • 1





    you never return a value from Read

    – sebastian
    Mar 27 at 11:17












  • @sebastian Thank you for reply, What value should I return in "Read()" function, I have tried to return "adata" but it is giving me an error "Error[Pe120]: return value type ("int16_t *") does not match the function type ("int16_t")" even after trying to return "buffer"

    – user11265782
    Mar 27 at 11:24












  • you never return a value in Compare, you just set a local array never used after, and never do something near of a compare, why the name 'Compare' ? Compare do not use its parameters. In Read you set also the local array adata for nothing

    – bruno
    Mar 27 at 11:35












  • @bruno I have made changes based on your comments, but unable to get the values of "buffer" in "Compare()" function. it is showing garbage values.........

    – user11265782
    Mar 27 at 11:47

















0















I am developing a code in which I need to read value from one function ("Read()") into another function ("compare") using pointer. Need to access values of "buffer" present in function "Read()". I have tried below approach, I am new with pointers implementation so not able to try much. "buffer" will have 9 elements which I have copied in "adata" in "Read()" function, need to have that data in "Compare" function.
Below is my code snippet:



 int16_t *Read (uint8_t *buffer, const uint16_t length)

static uint32_t totalBytes = 0;
static uint32_t respNo = 0;
int i;

GPIO_ClearValue(PMAP_GPIO_PORT_DIR_RS485, PMAP_GPIO_PIN_DIR_RS485);

UartRxFlush(UARW_UART_INDEX_RS485); //this function needs to be executed after the GPIO_ClearValue in order to remove the "zero" value from the buffer
respNo++;

int counter = 0; // counts n. valid bytes put in slave response buffer[]
do

OSTimeDly(2);

int8_t newBytesRcv = UartReceive(UARW_UART_INDEX_RS485,
(uint8_t*)&(buffer[counter]), length-counter);
totalBytes += newBytesRcv;
counter = totalBytes;

while (counter < length);

totalBytes = 0;
printf("n");
printf("Byte received........");
int16_t adata[9] = 0x09,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30;

for (i=0; i<9;i++)

printf("%d ",buffer[i]);
adata[i] = buffer[i];

printf("n");



int16_t Compare(uint8_t * message, uint16_t len)

rs485_message_t rs485Msg;
int StP15Vmsg[9] = 0x09,0x31,0x33,0x30,0x30,0x30,0x30,0x30,0x34;
int adata[9] = 0x09,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30;
uint8_t buffer[9];
const uint8_t length;
for (int j=0; j<=9; j++)

adata[j] = *Read(buffer, j);

if(compareArray(FWmsg,arduinodata,7)==0)


char res;
uint8_t add, fwcommand, fwaction;
uint16_t fwvalue;
rs485_message_t rs485Msg;
proto485ComposeMsg(&rs485Msg, &add, &fwcommand, &fwaction, &fwvalue);
res = GetABFWversion(&add, &fwcommand, &fwaction, &fwvalue);




Please guide....



Thank you










share|improve this question





















  • 1





    In your Compare function you go out of bounds of the adata array.

    – Some programmer dude
    Mar 27 at 11:14






  • 1





    you never return a value from Read

    – sebastian
    Mar 27 at 11:17












  • @sebastian Thank you for reply, What value should I return in "Read()" function, I have tried to return "adata" but it is giving me an error "Error[Pe120]: return value type ("int16_t *") does not match the function type ("int16_t")" even after trying to return "buffer"

    – user11265782
    Mar 27 at 11:24












  • you never return a value in Compare, you just set a local array never used after, and never do something near of a compare, why the name 'Compare' ? Compare do not use its parameters. In Read you set also the local array adata for nothing

    – bruno
    Mar 27 at 11:35












  • @bruno I have made changes based on your comments, but unable to get the values of "buffer" in "Compare()" function. it is showing garbage values.........

    – user11265782
    Mar 27 at 11:47













0












0








0








I am developing a code in which I need to read value from one function ("Read()") into another function ("compare") using pointer. Need to access values of "buffer" present in function "Read()". I have tried below approach, I am new with pointers implementation so not able to try much. "buffer" will have 9 elements which I have copied in "adata" in "Read()" function, need to have that data in "Compare" function.
Below is my code snippet:



 int16_t *Read (uint8_t *buffer, const uint16_t length)

static uint32_t totalBytes = 0;
static uint32_t respNo = 0;
int i;

GPIO_ClearValue(PMAP_GPIO_PORT_DIR_RS485, PMAP_GPIO_PIN_DIR_RS485);

UartRxFlush(UARW_UART_INDEX_RS485); //this function needs to be executed after the GPIO_ClearValue in order to remove the "zero" value from the buffer
respNo++;

int counter = 0; // counts n. valid bytes put in slave response buffer[]
do

OSTimeDly(2);

int8_t newBytesRcv = UartReceive(UARW_UART_INDEX_RS485,
(uint8_t*)&(buffer[counter]), length-counter);
totalBytes += newBytesRcv;
counter = totalBytes;

while (counter < length);

totalBytes = 0;
printf("n");
printf("Byte received........");
int16_t adata[9] = 0x09,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30;

for (i=0; i<9;i++)

printf("%d ",buffer[i]);
adata[i] = buffer[i];

printf("n");



int16_t Compare(uint8_t * message, uint16_t len)

rs485_message_t rs485Msg;
int StP15Vmsg[9] = 0x09,0x31,0x33,0x30,0x30,0x30,0x30,0x30,0x34;
int adata[9] = 0x09,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30;
uint8_t buffer[9];
const uint8_t length;
for (int j=0; j<=9; j++)

adata[j] = *Read(buffer, j);

if(compareArray(FWmsg,arduinodata,7)==0)


char res;
uint8_t add, fwcommand, fwaction;
uint16_t fwvalue;
rs485_message_t rs485Msg;
proto485ComposeMsg(&rs485Msg, &add, &fwcommand, &fwaction, &fwvalue);
res = GetABFWversion(&add, &fwcommand, &fwaction, &fwvalue);




Please guide....



Thank you










share|improve this question
















I am developing a code in which I need to read value from one function ("Read()") into another function ("compare") using pointer. Need to access values of "buffer" present in function "Read()". I have tried below approach, I am new with pointers implementation so not able to try much. "buffer" will have 9 elements which I have copied in "adata" in "Read()" function, need to have that data in "Compare" function.
Below is my code snippet:



 int16_t *Read (uint8_t *buffer, const uint16_t length)

static uint32_t totalBytes = 0;
static uint32_t respNo = 0;
int i;

GPIO_ClearValue(PMAP_GPIO_PORT_DIR_RS485, PMAP_GPIO_PIN_DIR_RS485);

UartRxFlush(UARW_UART_INDEX_RS485); //this function needs to be executed after the GPIO_ClearValue in order to remove the "zero" value from the buffer
respNo++;

int counter = 0; // counts n. valid bytes put in slave response buffer[]
do

OSTimeDly(2);

int8_t newBytesRcv = UartReceive(UARW_UART_INDEX_RS485,
(uint8_t*)&(buffer[counter]), length-counter);
totalBytes += newBytesRcv;
counter = totalBytes;

while (counter < length);

totalBytes = 0;
printf("n");
printf("Byte received........");
int16_t adata[9] = 0x09,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30;

for (i=0; i<9;i++)

printf("%d ",buffer[i]);
adata[i] = buffer[i];

printf("n");



int16_t Compare(uint8_t * message, uint16_t len)

rs485_message_t rs485Msg;
int StP15Vmsg[9] = 0x09,0x31,0x33,0x30,0x30,0x30,0x30,0x30,0x34;
int adata[9] = 0x09,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30;
uint8_t buffer[9];
const uint8_t length;
for (int j=0; j<=9; j++)

adata[j] = *Read(buffer, j);

if(compareArray(FWmsg,arduinodata,7)==0)


char res;
uint8_t add, fwcommand, fwaction;
uint16_t fwvalue;
rs485_message_t rs485Msg;
proto485ComposeMsg(&rs485Msg, &add, &fwcommand, &fwaction, &fwvalue);
res = GetABFWversion(&add, &fwcommand, &fwaction, &fwvalue);




Please guide....



Thank you







c






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 27 at 11:54







user11265782

















asked Mar 27 at 11:12









user11265782user11265782

32 bronze badges




32 bronze badges










  • 1





    In your Compare function you go out of bounds of the adata array.

    – Some programmer dude
    Mar 27 at 11:14






  • 1





    you never return a value from Read

    – sebastian
    Mar 27 at 11:17












  • @sebastian Thank you for reply, What value should I return in "Read()" function, I have tried to return "adata" but it is giving me an error "Error[Pe120]: return value type ("int16_t *") does not match the function type ("int16_t")" even after trying to return "buffer"

    – user11265782
    Mar 27 at 11:24












  • you never return a value in Compare, you just set a local array never used after, and never do something near of a compare, why the name 'Compare' ? Compare do not use its parameters. In Read you set also the local array adata for nothing

    – bruno
    Mar 27 at 11:35












  • @bruno I have made changes based on your comments, but unable to get the values of "buffer" in "Compare()" function. it is showing garbage values.........

    – user11265782
    Mar 27 at 11:47












  • 1





    In your Compare function you go out of bounds of the adata array.

    – Some programmer dude
    Mar 27 at 11:14






  • 1





    you never return a value from Read

    – sebastian
    Mar 27 at 11:17












  • @sebastian Thank you for reply, What value should I return in "Read()" function, I have tried to return "adata" but it is giving me an error "Error[Pe120]: return value type ("int16_t *") does not match the function type ("int16_t")" even after trying to return "buffer"

    – user11265782
    Mar 27 at 11:24












  • you never return a value in Compare, you just set a local array never used after, and never do something near of a compare, why the name 'Compare' ? Compare do not use its parameters. In Read you set also the local array adata for nothing

    – bruno
    Mar 27 at 11:35












  • @bruno I have made changes based on your comments, but unable to get the values of "buffer" in "Compare()" function. it is showing garbage values.........

    – user11265782
    Mar 27 at 11:47







1




1





In your Compare function you go out of bounds of the adata array.

– Some programmer dude
Mar 27 at 11:14





In your Compare function you go out of bounds of the adata array.

– Some programmer dude
Mar 27 at 11:14




1




1





you never return a value from Read

– sebastian
Mar 27 at 11:17






you never return a value from Read

– sebastian
Mar 27 at 11:17














@sebastian Thank you for reply, What value should I return in "Read()" function, I have tried to return "adata" but it is giving me an error "Error[Pe120]: return value type ("int16_t *") does not match the function type ("int16_t")" even after trying to return "buffer"

– user11265782
Mar 27 at 11:24






@sebastian Thank you for reply, What value should I return in "Read()" function, I have tried to return "adata" but it is giving me an error "Error[Pe120]: return value type ("int16_t *") does not match the function type ("int16_t")" even after trying to return "buffer"

– user11265782
Mar 27 at 11:24














you never return a value in Compare, you just set a local array never used after, and never do something near of a compare, why the name 'Compare' ? Compare do not use its parameters. In Read you set also the local array adata for nothing

– bruno
Mar 27 at 11:35






you never return a value in Compare, you just set a local array never used after, and never do something near of a compare, why the name 'Compare' ? Compare do not use its parameters. In Read you set also the local array adata for nothing

– bruno
Mar 27 at 11:35














@bruno I have made changes based on your comments, but unable to get the values of "buffer" in "Compare()" function. it is showing garbage values.........

– user11265782
Mar 27 at 11:47





@bruno I have made changes based on your comments, but unable to get the values of "buffer" in "Compare()" function. it is showing garbage values.........

– user11265782
Mar 27 at 11:47












1 Answer
1






active

oldest

votes


















0














Note : this is not stricto sensu an 'answer' but more a list of remarks




About Read



the signature is




int16_t *Read (uint8_t *buffer, const uint16_t length)



but the function never return a value, probably you want to return the number of read bytes, in that case



int16_t Read (uint8_t *buffer, const uint16_t length)



The static variable respNo is only incremented and never used, these two lines can be removed :




static uint32_t respNo = 0;
respNo++;




There is no reason to have totalBytes static because you reset its value to 0 before to print the buffer, so there is no historic from a call to the next. It must be local.



counter is useless because it always value totalBytes




UartReceive always success and never returns a value indicating an error ?



  • if yes it is useless Read returns a value because it will always be length

  • if no you have to take into account the error and probably Read have to return totalBytes valuing the number of read bytes


In




 printf("n");
printf("Byte received........");
int16_t adata[9] = 0x09,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30;

for (i=0; i<9;i++)

printf("%d ",buffer[i]);
adata[i] = buffer[i];

printf("n");



  • you loop from 1 to 9 rather than from 0 to 8 so you at least go out of adata and you do not print the value of the first byte in buffer

  • you only set adata and you never use it, it can be removed.

  • the format must be %u rather than %d

Probably the loop must be from 0 to totalBytes - 1 to be compatible with the code above :




 printf("nByte received : ");

for (i=0; i<totalBytes;i++)

printf("%u ",buffer[i]);

putchar('n');




About Compare



In




 rs485_message_t rs485Msg;
int StP15Vmsg[9] = 0x09,0x31,0x33,0x30,0x30,0x30,0x30,0x30,0x34;
int adata[9] = 0x09,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30;
uint8_t buffer[9];
const uint8_t length;
for (int j=0; j<=9; j++)

adata[j] = *Read(buffer, j);




rs485Msg and StP15Vmsg are useless, remove them



I do not understand why you call Read several times in the loop with a length incremented each time, so you will rewrite several times on the buffer. I do not understand too while you save the return value of Read (who do return a value currently)



Probably you want just that :



uint8_t buffer[9];
uint8_t length = Read(buffer, 9);


In




if(compareArray(FWmsg,arduinodata,7)==0)

char res;
uint8_t add, fwcommand, fwaction;
uint16_t fwvalue;
rs485_message_t rs485Msg;
proto485ComposeMsg(&rs485Msg, &add, &fwcommand, &fwaction, &fwvalue);
res = GetABFWversion(&add, &fwcommand, &fwaction, &fwvalue);




I do not a link with the code above, and res is set but never used




Compare never does a comparison, why it is named Compare ?
It never returns a value, what value is it supposed to return ?






share|improve this answer



























  • Thank you for help........... it has solve the purpose

    – user11265782
    Mar 27 at 17:23










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/3.0/"u003ecc by-sa 3.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
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55375822%2fneed-to-read-value-of-array-elements-from-one-function-inside-another-function-u%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









0














Note : this is not stricto sensu an 'answer' but more a list of remarks




About Read



the signature is




int16_t *Read (uint8_t *buffer, const uint16_t length)



but the function never return a value, probably you want to return the number of read bytes, in that case



int16_t Read (uint8_t *buffer, const uint16_t length)



The static variable respNo is only incremented and never used, these two lines can be removed :




static uint32_t respNo = 0;
respNo++;




There is no reason to have totalBytes static because you reset its value to 0 before to print the buffer, so there is no historic from a call to the next. It must be local.



counter is useless because it always value totalBytes




UartReceive always success and never returns a value indicating an error ?



  • if yes it is useless Read returns a value because it will always be length

  • if no you have to take into account the error and probably Read have to return totalBytes valuing the number of read bytes


In




 printf("n");
printf("Byte received........");
int16_t adata[9] = 0x09,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30;

for (i=0; i<9;i++)

printf("%d ",buffer[i]);
adata[i] = buffer[i];

printf("n");



  • you loop from 1 to 9 rather than from 0 to 8 so you at least go out of adata and you do not print the value of the first byte in buffer

  • you only set adata and you never use it, it can be removed.

  • the format must be %u rather than %d

Probably the loop must be from 0 to totalBytes - 1 to be compatible with the code above :




 printf("nByte received : ");

for (i=0; i<totalBytes;i++)

printf("%u ",buffer[i]);

putchar('n');




About Compare



In




 rs485_message_t rs485Msg;
int StP15Vmsg[9] = 0x09,0x31,0x33,0x30,0x30,0x30,0x30,0x30,0x34;
int adata[9] = 0x09,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30;
uint8_t buffer[9];
const uint8_t length;
for (int j=0; j<=9; j++)

adata[j] = *Read(buffer, j);




rs485Msg and StP15Vmsg are useless, remove them



I do not understand why you call Read several times in the loop with a length incremented each time, so you will rewrite several times on the buffer. I do not understand too while you save the return value of Read (who do return a value currently)



Probably you want just that :



uint8_t buffer[9];
uint8_t length = Read(buffer, 9);


In




if(compareArray(FWmsg,arduinodata,7)==0)

char res;
uint8_t add, fwcommand, fwaction;
uint16_t fwvalue;
rs485_message_t rs485Msg;
proto485ComposeMsg(&rs485Msg, &add, &fwcommand, &fwaction, &fwvalue);
res = GetABFWversion(&add, &fwcommand, &fwaction, &fwvalue);




I do not a link with the code above, and res is set but never used




Compare never does a comparison, why it is named Compare ?
It never returns a value, what value is it supposed to return ?






share|improve this answer



























  • Thank you for help........... it has solve the purpose

    – user11265782
    Mar 27 at 17:23















0














Note : this is not stricto sensu an 'answer' but more a list of remarks




About Read



the signature is




int16_t *Read (uint8_t *buffer, const uint16_t length)



but the function never return a value, probably you want to return the number of read bytes, in that case



int16_t Read (uint8_t *buffer, const uint16_t length)



The static variable respNo is only incremented and never used, these two lines can be removed :




static uint32_t respNo = 0;
respNo++;




There is no reason to have totalBytes static because you reset its value to 0 before to print the buffer, so there is no historic from a call to the next. It must be local.



counter is useless because it always value totalBytes




UartReceive always success and never returns a value indicating an error ?



  • if yes it is useless Read returns a value because it will always be length

  • if no you have to take into account the error and probably Read have to return totalBytes valuing the number of read bytes


In




 printf("n");
printf("Byte received........");
int16_t adata[9] = 0x09,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30;

for (i=0; i<9;i++)

printf("%d ",buffer[i]);
adata[i] = buffer[i];

printf("n");



  • you loop from 1 to 9 rather than from 0 to 8 so you at least go out of adata and you do not print the value of the first byte in buffer

  • you only set adata and you never use it, it can be removed.

  • the format must be %u rather than %d

Probably the loop must be from 0 to totalBytes - 1 to be compatible with the code above :




 printf("nByte received : ");

for (i=0; i<totalBytes;i++)

printf("%u ",buffer[i]);

putchar('n');




About Compare



In




 rs485_message_t rs485Msg;
int StP15Vmsg[9] = 0x09,0x31,0x33,0x30,0x30,0x30,0x30,0x30,0x34;
int adata[9] = 0x09,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30;
uint8_t buffer[9];
const uint8_t length;
for (int j=0; j<=9; j++)

adata[j] = *Read(buffer, j);




rs485Msg and StP15Vmsg are useless, remove them



I do not understand why you call Read several times in the loop with a length incremented each time, so you will rewrite several times on the buffer. I do not understand too while you save the return value of Read (who do return a value currently)



Probably you want just that :



uint8_t buffer[9];
uint8_t length = Read(buffer, 9);


In




if(compareArray(FWmsg,arduinodata,7)==0)

char res;
uint8_t add, fwcommand, fwaction;
uint16_t fwvalue;
rs485_message_t rs485Msg;
proto485ComposeMsg(&rs485Msg, &add, &fwcommand, &fwaction, &fwvalue);
res = GetABFWversion(&add, &fwcommand, &fwaction, &fwvalue);




I do not a link with the code above, and res is set but never used




Compare never does a comparison, why it is named Compare ?
It never returns a value, what value is it supposed to return ?






share|improve this answer



























  • Thank you for help........... it has solve the purpose

    – user11265782
    Mar 27 at 17:23













0












0








0







Note : this is not stricto sensu an 'answer' but more a list of remarks




About Read



the signature is




int16_t *Read (uint8_t *buffer, const uint16_t length)



but the function never return a value, probably you want to return the number of read bytes, in that case



int16_t Read (uint8_t *buffer, const uint16_t length)



The static variable respNo is only incremented and never used, these two lines can be removed :




static uint32_t respNo = 0;
respNo++;




There is no reason to have totalBytes static because you reset its value to 0 before to print the buffer, so there is no historic from a call to the next. It must be local.



counter is useless because it always value totalBytes




UartReceive always success and never returns a value indicating an error ?



  • if yes it is useless Read returns a value because it will always be length

  • if no you have to take into account the error and probably Read have to return totalBytes valuing the number of read bytes


In




 printf("n");
printf("Byte received........");
int16_t adata[9] = 0x09,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30;

for (i=0; i<9;i++)

printf("%d ",buffer[i]);
adata[i] = buffer[i];

printf("n");



  • you loop from 1 to 9 rather than from 0 to 8 so you at least go out of adata and you do not print the value of the first byte in buffer

  • you only set adata and you never use it, it can be removed.

  • the format must be %u rather than %d

Probably the loop must be from 0 to totalBytes - 1 to be compatible with the code above :




 printf("nByte received : ");

for (i=0; i<totalBytes;i++)

printf("%u ",buffer[i]);

putchar('n');




About Compare



In




 rs485_message_t rs485Msg;
int StP15Vmsg[9] = 0x09,0x31,0x33,0x30,0x30,0x30,0x30,0x30,0x34;
int adata[9] = 0x09,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30;
uint8_t buffer[9];
const uint8_t length;
for (int j=0; j<=9; j++)

adata[j] = *Read(buffer, j);




rs485Msg and StP15Vmsg are useless, remove them



I do not understand why you call Read several times in the loop with a length incremented each time, so you will rewrite several times on the buffer. I do not understand too while you save the return value of Read (who do return a value currently)



Probably you want just that :



uint8_t buffer[9];
uint8_t length = Read(buffer, 9);


In




if(compareArray(FWmsg,arduinodata,7)==0)

char res;
uint8_t add, fwcommand, fwaction;
uint16_t fwvalue;
rs485_message_t rs485Msg;
proto485ComposeMsg(&rs485Msg, &add, &fwcommand, &fwaction, &fwvalue);
res = GetABFWversion(&add, &fwcommand, &fwaction, &fwvalue);




I do not a link with the code above, and res is set but never used




Compare never does a comparison, why it is named Compare ?
It never returns a value, what value is it supposed to return ?






share|improve this answer















Note : this is not stricto sensu an 'answer' but more a list of remarks




About Read



the signature is




int16_t *Read (uint8_t *buffer, const uint16_t length)



but the function never return a value, probably you want to return the number of read bytes, in that case



int16_t Read (uint8_t *buffer, const uint16_t length)



The static variable respNo is only incremented and never used, these two lines can be removed :




static uint32_t respNo = 0;
respNo++;




There is no reason to have totalBytes static because you reset its value to 0 before to print the buffer, so there is no historic from a call to the next. It must be local.



counter is useless because it always value totalBytes




UartReceive always success and never returns a value indicating an error ?



  • if yes it is useless Read returns a value because it will always be length

  • if no you have to take into account the error and probably Read have to return totalBytes valuing the number of read bytes


In




 printf("n");
printf("Byte received........");
int16_t adata[9] = 0x09,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30;

for (i=0; i<9;i++)

printf("%d ",buffer[i]);
adata[i] = buffer[i];

printf("n");



  • you loop from 1 to 9 rather than from 0 to 8 so you at least go out of adata and you do not print the value of the first byte in buffer

  • you only set adata and you never use it, it can be removed.

  • the format must be %u rather than %d

Probably the loop must be from 0 to totalBytes - 1 to be compatible with the code above :




 printf("nByte received : ");

for (i=0; i<totalBytes;i++)

printf("%u ",buffer[i]);

putchar('n');




About Compare



In




 rs485_message_t rs485Msg;
int StP15Vmsg[9] = 0x09,0x31,0x33,0x30,0x30,0x30,0x30,0x30,0x34;
int adata[9] = 0x09,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30;
uint8_t buffer[9];
const uint8_t length;
for (int j=0; j<=9; j++)

adata[j] = *Read(buffer, j);




rs485Msg and StP15Vmsg are useless, remove them



I do not understand why you call Read several times in the loop with a length incremented each time, so you will rewrite several times on the buffer. I do not understand too while you save the return value of Read (who do return a value currently)



Probably you want just that :



uint8_t buffer[9];
uint8_t length = Read(buffer, 9);


In




if(compareArray(FWmsg,arduinodata,7)==0)

char res;
uint8_t add, fwcommand, fwaction;
uint16_t fwvalue;
rs485_message_t rs485Msg;
proto485ComposeMsg(&rs485Msg, &add, &fwcommand, &fwaction, &fwvalue);
res = GetABFWversion(&add, &fwcommand, &fwaction, &fwvalue);




I do not a link with the code above, and res is set but never used




Compare never does a comparison, why it is named Compare ?
It never returns a value, what value is it supposed to return ?







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 27 at 13:01

























answered Mar 27 at 12:28









brunobruno

19.3k3 gold badges16 silver badges28 bronze badges




19.3k3 gold badges16 silver badges28 bronze badges















  • Thank you for help........... it has solve the purpose

    – user11265782
    Mar 27 at 17:23

















  • Thank you for help........... it has solve the purpose

    – user11265782
    Mar 27 at 17:23
















Thank you for help........... it has solve the purpose

– user11265782
Mar 27 at 17:23





Thank you for help........... it has solve the purpose

– user11265782
Mar 27 at 17:23








Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















draft saved

draft discarded
















































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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55375822%2fneed-to-read-value-of-array-elements-from-one-function-inside-another-function-u%23new-answer', 'question_page');

);

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







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현