The problem I'm facing has to do with intptr_t data type and the way fprintf() takes arguments for the %.*s format. The %.*s format expect field precision to have type int, and maybe that's not unreasonable per se.
Not in this case though:
#include <stdio.h>
#include <stdint.h>
int main() {
char fname[] = "Some_File.txt";
FILE *write = fopen(fname, "w");
if (write != NULL) {
printf("\n\tType below :\n\n");
char in[501] = ""; char *p;
while (1) {
fgets(in, MAX_LN, stdin);
/*** Region with compiler warnings begins ***/
if ((p = strstr(in, "/end/")) != 0) {
intptr_t o = p - in;
fprintf(write, "%.*s", o, in);
/*** Region with compiler warnings ends ***/
fclose(write);
break;
} else {
fputs(in, write);
}
}
}
}
If I compile this, it doesn't play well with
%.*s, and the compiler points that out:warning: field precision should have type 'int', but argument has type 'intptr_t' (aka 'long') [-Wformat]
If I make it
int o;, it plays well with%.*sbut of course isn't ideal, and the compiler says as much:warning: implicit conversion loses integer precision: 'long' to 'int' [-Wshorten-64-to-32]
Now, this is demo code, and the max size that o can hold is 500 here, however, in my actual code, it can be 10,000 or even 100,000 (still very much within the size of a 32-bit int, isn't it?)
So what will resolve this best with the least changes?
Compiled on Clang (might be very similar on GCC) with -Wall -Wextra -pedantic.