错误发生在行:错误打印的数组用C
printf("\n%s was found at word number(s): %d\n", search_for, pos);
我想打印我的整数(pos
)的数组,但我不知道怎么做。我通过命令行运行它,我得到这个错误:
search_word.c:57:28: warning: format specifies type 'int' but the argument hastype 'int *' [-Wformat] search_for, pos);
代码:
const int MAX_STRING_LEN = 100;
void Usage(char prog_name[]);
int main(int argc, char* argv[]) {
char search_for[MAX_STRING_LEN];
char current_word[MAX_STRING_LEN];
int scanf_rv;
int loc = 0;
int pos[MAX_STRING_LEN] = {0};
int word_count = 0;
int freq = 0;
/* Check that the user-specified word is on the command line */
if (argc != 2) Usage(argv[0]);
strcpy(search_for, argv[1]);
printf("Enter the text to be searched\n");
scanf_rv = scanf("%s", current_word);
while (scanf_rv != EOF && strcmp(current_word, search_for) != MAX_STRING_LEN) {
if (strcmp(current_word, search_for) == 0) {
loc++;
freq++;
pos[loc] = word_count;
}
word_count++;
scanf_rv = scanf("%s", current_word);
}
if (freq == 0)
printf("\n%s was not found in the %d words of input\n",
search_for, word_count);
else
printf("\n%s was found at word number(s): %d\n",
search_for, pos);
printf("The frequency of the word was: %d\n", freq);
return 0;
} /* main */
/* If user-specified word isn't on the command line,
* print a message and quit
*/
void Usage(char prog_name[]) {
fprintf(stderr, "usage: %s <string to search for>\n",
prog_name);
exit(0);
} /* Usage */
就像编译器告诉你的那样,pos是一个指向int的指针,而不是int。这是一个完整的阵列。你想打印哪一个? – 5gon12eder 2014-09-12 21:06:51
我想打印已存储的整个整数数组 – 2014-09-12 21:07:53
然后您需要使用循环。 – Barmar 2014-09-12 21:08:14