【问题标题】:Removing Occurences of String from another in C (Code and Error Messages)在 C 中从另一个字符串中删除出现的字符串(代码和错误消息)
【发布时间】: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


【解决方案1】:
  1. 警告 C4018:“

我不知道该警告在哪一行,但我认为它在这里:

for (i = 0; i < strlen(list1); i++)

strlen 返回一个size_t 变量,它是一个unsigned int。您将其与有符号变量进行比较,这可能会给您带来一些麻烦。例如,考虑以下声明:

int a = -1;
unsigned int b = 100;

比较 a &lt; b 将是错误的。这是因为 a 被转换为无符号并且由于负数的表示方式(通常是two's complement)。但是,在您的情况下,这不是问题。为避免收到警告,请将i 声明为unsigned int。

  1. 警告 C4033:“str_check”必须返回一个值

str_check的声明是:

char str_check(char* list1, char* list2);

但是,您不会在此函数的定义中返回任何 char。要么你应该声明它void str_check(char* list1, char* list2);,要么你应该从它返回一些值。

  1. 警告 C4101:“k”:未引用的局部变量

这个很简单:你声明了一个变量k,但你没有使用它。


其余部分与上述重复(最后一个除外)。除了第二个警告,它可能会导致你一些意外的行为,程序应该可以完美地运行。

最后一个错误是你的问题。我将尝试为您提供一些有关错误含义的背景信息以及一些希望对您有所帮助的参考资料。

你有一个源代码。为了从中创建一个可执行文件,必须采取一些步骤:

  1. 预处理。

在此步骤中分析预处理器指令。在您的情况下,您 include 一些文件使用 #include 指令。当预处理器遇到这个指令时,它只是将该文件的内容复制到您的文件中。

  1. 编译。

此步骤为您的程序(CPU 可以理解的程序)生成实际的目标代码。但是,有一些函数,例如getchar,您没有提供任何定义。它们已经在库中编译。

  1. 正在链接。

链接器的工作是链接您在最后一步生成的目标代码与所需的库,以便可以调用您使用的所有函数。此步骤会产生您的错误。它基本上说它找不到对某物的引用。在这里,您将找到有关此错误的一些文档:

"1 unresolved externals" C++ http://support.microsoft.com/kb/815650 http://www.dreamincode.net/forums/topic/22701-fatal-error-lnk1120-1-unresolved-externals/

【讨论】:

  • 感谢帮助,但我还是不明白为什么isPrefix是一个未解析的外部,程序应该能够将函数定义连接到函数调用。
  • 嗯...您还没有定义它(至少在您发布的代码中没有),它不在您包含的标准库中。
猜你喜欢
  • 1970-01-01
  • 2023-01-28
  • 1970-01-01
  • 2021-01-28
  • 1970-01-01
  • 1970-01-01
  • 2011-12-19
  • 2014-07-16
  • 1970-01-01
相关资源
最近更新 更多