I’m trying to write a unsigned char* array to a file.
A minimal working example of the code that I’ve tried so far is (assume fp is correctly initialised):
unsigned char* x; int i; int j; int sizeOfx;
for (i=0; i<n; i++) {
x = // getter function with parameter i
sizeOfx = // getter function that returns the number of elements in x
for (j=0; j<sizeOfx; j++) {
fprintf(fp,"%s",x[j]);
}
}
i.e. I’m going through the char array one element at a time and writing it to the file.
However, I get the error
format ‘%s’ expects argument of type ‘char*’, but argument 3 has type ‘int’ [-Wformat]
How can I fix this?
Thank you very much in advance!
%sis used to print a string, so you would need to changex[j]toxto ‘fix’ your error.As you really seem to want to write each
charseparately, you need to think how you want to store the ‘elements’ (characters of the string).You can use
%cto store their value as an ASCII value in the file (which is basically identical when using%sandx, unless you want to write more/less than the complete string).Or you can store the ‘element values’ as integers, ie textual values using the characters 0-9, using
%d. Or maybe hexadecimal using%xusing the characters 0-9 and a-f.So it is up to you how you want to store the ‘elements’ of
x.