【发布时间】:2014-11-09 00:40:24
【问题描述】:
谁能告诉我我的代码有什么问题?我已经广泛查看了 VS 给我的错误消息,但我很茫然。我已附上错误消息以获取更多信息。
//Inclusion of necessary header files
#include <stdio.h>
#include <string.h>
//String Removal function prototype
char str_check(char *, char *);
void str_remove(char*, char*);
int isPrefix(char* list1, char* list2);
//Entry point for header files, pre-processor directives, and function prototypes
int main()
{
//Necessary variable declarations
char list1[500];
char list2[20];
//Input prompt and input function for first string
printf("Please enter a string of up to 500 characters for first array.\n");
*list1 = getchar();
//Input prompt and input function for second string
printf("Please enter a string of up to 20 characters for second array.\n");
*list2 = getchar();
//String Removal function call assigns function output to a third array and uses string function to display output
*list1 = str_check(list1, list2);
puts(list1);
//Exit sequence
return 0;
}
//String check function definition
char str_check(char* list1, char* list2)
{
//Temporary character pointer declarations and for loop control variables
char *ptr1=list1, *ptr2=list2;
int i, j, k;
//Loop checks both array elements for concurrent characters using isPrefix function, checks for concurrent string segments if one is found, call string removal function
for (i = 0; i < strlen(list1); i++)
{
if (isPrefix(ptr1+i, ptr2)==1)
{
str_remove(ptr1+i, ptr2);
}
}
return;
}
//isPrefix function definition checks for string occurences
int isPrefix_1 (char* ptr1, char* ptr2){
int i=0;
while(*(ptr1 + i) != '\0')
{
if(*(ptr1 + i) != *(ptr2 + i))
{
return 0;
}
i++;
}
return 1;
}
void str_remove(char* ptr1, char* ptr2)
{
int offset = strlen(ptr2);
int i=0;
for(i=0;i<(strlen(ptr1)-strlen(ptr2));i++)
{
*(ptr1+i) = *(ptr1+i+offset);
}
*(ptr1+i+offset) ='\0';
}
ERROR MESSAGES:
: warning C4018: '<' : signed/unsigned mismatch
: warning C4033: 'str_check' must return a value
: warning C4101: 'k' : unreferenced local variable
: warning C4101: 'j' : unreferenced local variable
: warning C4018: '<' : signed/unsigned mismatch
: warning C4716: 'str_check' : must return a value
: fatal error LNK1120: 1 unresolved externals
【问题讨论】:
-
关于此行和类似行:*list1 = getchar(); getchar() 函数没有得到一堆字符。相反,它只得到一个字符。您可以尝试使用 fgets() 来获取整个字符串,甚至在输入字符串的末尾设置空字符。但是,它会停止(在使用后)换行符或遇到 EOF 事件。
-
这一行:*list1 = str_check(list1, list2);将失败,因为它在 list1 数组的前几个字节中设置了指向 str_check() 返回的内容的指针。可能不是你想做的。
-
所有“错误”消息,除了最后一条,都是编译时错误,需要更正才能获得干净的编译。最后一个“错误”消息由链接器输出,由于编译已经失败,因此可以忽略。
-
我曾经使用过的每个“C”编译器都会输出文件名和行号(有时还会输出发现错误/警告的列号)。某些 IDE 已设置,因此单击错误消息会将编辑器移动到错误/警告的位置。即使那样,这些错误/警告消息也包含文件行号作为消息的一部分。你用什么编译器跳过输出这些基本信息?
-
ptr2(一个可怕的、无信息的名称)可能会在某些循环期间超出 list2 数组,例如在 isPrefix_1() 函数中(不在任何地方调用,也调用 isPrefix() 函数,但不会存在于提供的代码中。
标签: c find-occurrences