// Powered by Judge0
#include <stdio.h>
#include <string.h>
#include <stdint.h>
typedef struct {
char data[500];
uint32_t len;
} payload;
void at_start(payload* payloadData)
{
char p1[20] = "HI\r\n";
memcpy(payloadData->data, p1, 4);
payloadData->len = 4;
}
void at_net(payload* payloadData)
{
char p1[20] = "HI CMDCMD\r\n";
memcpy(payloadData->data, p1, 14);
payloadData->len = 14;
}
int main(void) {
payload* payloadData;
at_start(payloadData);
printf("Data : %s \n", payloadData->data);
at_net(payloadData);
printf("Data : %s \n", payloadData->data);
at_start(payloadData);
printf("Data : %s \n", payloadData->data);
printf("Hello Judge0!\n");
return 0;
}
can you help to figure out memcpy function issue when multiple times used and how to used data with dynamic length
issue when used data length was changed based on function.
issue is output got wrong.
Data : HI
Data : HI CMDCMD
Data : HI
MDCMD
last printf get extra \r\n
payloadData
without initializationlen
member is not read and data is simply printed until the end of string (null character'\0'
)Try this: