I am using C. I am having issues with using pointers for the fscanf function. When I try to do:
int *x;
/* ... */
fscanf(file, "%d", x[i]);
My compiler gives me a warning saying "format argument is not a pointer" and the code just doesn't run (I get a message saying "Water.exe has stopped working"). If I replace x with *x, it just doesn't compile... Is this just a syntax issue?
If you want to read a single integer, do this:
int x;
fscanf(file, "%d", &x );
If you want, you could do this to read a single integer in a dynamically-allocated variable:
int *x = malloc(sizeof(int));
fscanf(file, "%d", x );
If you want an array of integers, do this:
int *x = malloc(sizeof(int) * DESIRED_ARRAY_SIZE);
fscanf(file, "%d", &x[i] );
%d expects a pointer to an int, but x[i] is an int, so you need to take the address of your list element using the address-of operator (unary &).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With