Reputation: 19
I have this C code:
#include <stdio.h>
#include <stdlib.h>
int main(){
char *bitstr;
printf("Enter a bitstring or q for quit: ");
scanf("%s", &bitstr);
return 0;
}
I keep receive the following error. What am I doing wrong?
warning: format '%s' expects argument of type 'char *', but
argument 2 has type 'char **' [-Wformat]
Upvotes: 1
Views: 12098
Reputation: 154601
1 Pass address of a char
array in scanf()
and not the address of a char*
.
2 Insure you do not overwrite your destination buffer.
3 Right-size your buffer needs. It is apparent from other posts you want a binary textual representation of an int
. Let's assume your int
is 8 bytes (64 bits).
#include <stdio.h>
#include <stdlib.h>
int main(){
char bitstr[8*8 + 1]; // size to a bit representation of a big integer.
printf("Enter a bitstring or q for quit: ");
//Change format and pass bitscr, this results in the address of bitscr array.
scanf("%64s", bitstr);
return 0;
}
I prefer the fgets() & sscanf() method.
char buf[100]; // You can re-use this buffer for other inputs.
if (fgets(buf, sizeof(buf), stdin) == NULL) { ; /*handle error or EOF */ }
sscanf(buf, "%64s", bitstr);
Upvotes: 1
Reputation: 8451
Try this:
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int main(){
char bitstr[MAX] = "";
printf("Enter a bitstring or q for quit: ");
scanf("%s", &bitstr);
// or fgets(bitstr);
return 0;
}
Try to define or allocate the size of your string/char array.
Upvotes: 0