In your example, as[]
has 3 elements (your real-life code may have a different number)
// Many more than this, but you get the general struct idea..
struct a as[][3] = {
{{ 0xF245, 5, 0x6F02C4 }},
{{ 0x471D, 128, 0x65892 }},
{{ 0x6198F, 12, 0xA4092 }}
}
However, you're using the number of elements in buffer
(which in a comment you say is char buffer[256]
to index it:
for (int i = 0; i <= (sizeof(buffer) / sizeof(buffer[0])); i++) {
memset(buffer, 0, sizeof(buffer));
fseek(fdin, as[i]->pos, SEEK_SET);
fread(buffer, 1, as[i]->sz, fdin);
fseek(fdout, as[i]->addr, SEEK_SET);
fwrite(buffer, 1, as[i]->sz, fdout);
}
Change the for
loop to (note also that the test is changed from <=
to <
):
for (int i = 0; i < (sizeof(as) / sizeof(as[0])); i++)
Finally - I think you're making things unnecessarily more complicated (and probably buggy) by using a 2 dimensional array for as
for no reason. try:
struct a as[] = {
{ 0xF245, 5, 0x6F02C4 },
{ 0x471D, 128, 0x65892 },
{ 0x6198F, 12, 0xA4092 }
}
// ...
for (int i = 0; i < (sizeof(as) / sizeof(as[0])); i++) {
memset(buffer, 0, sizeof(buffer));
fseek(fdin, as[i].pos, SEEK_SET);
fread(buffer, 1, as[i].sz, fdin);
fseek(fdout, as[i].addr, SEEK_SET);
fwrite(buffer, 1, as[i].sz, fdout);
}