I have this buffer which I use for store some different types of data:
int d;
char *current_Results;
char Results[1000];
current_Results = Results;
Now, I copy to this buffer the next data:
d = 50;
memcpy(current_Results, &d, sizeof(d));
printf("Current data is - %d\n", (int)(*current_Results)); // "Current data is - 50" Output OK!.
(current_Results)+= sizeof(d);
d = -20;
memcpy(current_Results, &d, sizeof(d));
printf("Current data is - %d\n", (int)(*current_Results)); // "Current data is - 1" Output WRONG!.
(current_Results)+= sizeof(d);
... and so on - wrong output later on...
What do I do wrong?
(int)(*current_Results)is not interpreting the bytes that are found at that location as an integer as you seem to expect. It is taking the one character found there and casts that to anint. You probably mean*(int*)current_Results.But all of what you are doing here may not be well defined because your are trying to access
int*and arbitrary alignments. Don’t do that.