The updated code doesn't use capacity in readfile(). The function doesn't explicitly report an error when it fails to open the file — that cost me some time.
Here's a variant of your code which produces the output I expect:
#include <stdio.h>
int printcontents(unsigned char storage[ ]);
int changecolor (unsigned char storage[ ]);
int readfile(unsigned char storage[ ], size_t capacity, char filename[ ]);
int main(void)
{
unsigned char data1[4000] = {0};
readfile(data1, 4000, "file.gif"); // filled 3022 indices in data1; file.gif exists
printcontents(data1); // prints the array contents as desired here
changecolor(data1); // doesn’t change the array contents
printcontents(data1); // prints the same array contents as original
return 0;
}
int printcontents(unsigned char storage[ ])
{
printf("%#X\n", storage[13]); // data1 passed in function call in main
return 0;
}
int changecolor (unsigned char storage[ ])
{
storage[13] = storage[13] >> 2; // data1 passed in function call in main
return 0;
}
int readfile(unsigned char storage[ ], size_t capacity, char filename[ ])
{
FILE * content = fopen(filename, "r");
if (content != NULL)
{
size_t numElements, bytesPer = sizeof(unsigned char);
fseek(content, 0, SEEK_END);
numElements = ftell(content);
if (numElements > capacity)
numElements = capacity;
rewind(content);
if (fread(storage, bytesPer, numElements, content) != numElements)
printf("Bogus input\n"); // returned 3022
printf("Got %zu bytes\n", numElements);
for (int i = 0; i < 16; i++)
printf(" 0x%.2X", storage[i]);
putchar('\n');
fclose(content);
}
else
fprintf(stderr, "failed to open file '%s'\n", filename);
return 0;
}
I used a random number generator to create the file.gif file. When I ran the program on that file, I got:
Got 3023 bytes
0x62 0x66 0x74 0x77 0x76 0x62 0x66 0x6E 0x6D 0x72 0x70 0x62 0x63 0x66 0x71 0x7A
0X66
0X19
As you can see, the value in position 13 of the array (between 0x63 and 0x71) changes from 0x66 (102) to 0x19 (25), which is the correct 'divide by 4' value.
It's not possible to guess from here what's going wrong in your version. The simplest guess is that the file file.gif isn't there, despite your assertions to the contrary. Your code doesn't show that the fread() succeeded.
Most of the changes I made were those necessary to get the code pass my default compiler warnings. I'm using clang from XCode 10.1 on Mojave 10.14.1, and I compiled arr41.c to arr41 using:
/usr/bin/clang -O3 -g -std=c11 -Wall -Wextra -Werror -Wmissing-prototypes \
-Wstrict-prototypes arr41.c -o arr41
I also added the diagnostic code to warn when the file wasn't opened, and to print the amount of data read, the first 16 bytes of that data, and tidied up the formatting of the printed values (end outputs with newlines, in general).
I note in passing that this is not an MCVE (Minimal, Complete, Verifiable Example). An MCVE might be:
#include <stdio.h>
static void printcontents(unsigned char storage[]);
static void changecolor(unsigned char storage[]);
int main(void)
{
unsigned char data1[20] = "ABCDEFGHIJKLMNOPQRS";
printcontents(data1);
changecolor(data1);
printcontents(data1);
return 0;
}
static void printcontents(unsigned char storage[])
{
printf("%#X\n", storage[13]);
}
static void changecolor(unsigned char storage[])
{
storage[13] = storage[13] >> 2;
}
Output:
0X4E
0X13
Again, this is clearly correct.
data1[13].data1array is initialized, includingdata1[13].4000in an MCVE;20would be sufficient, and could be reduced further if you changed13to, say,3. Of course, the problem must be reproducible on the small scale. Your code doesn't usecapacity1; it won't appear in an MCVE.data1[13] == 0before the call, it won't change after the call. Since you've not shown us how the variable is initialized, or what the outputs are, we can't tell whether that's part of the trouble.