c - 在 C 中删除另一个字符串中出现的字符串(代码和错误消息)

标签 c find-occurrences

有人可以告诉我我的代码有什么问题吗?我已经广泛地查看了 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

最佳答案

  1. 警告 C4018:“<”:有符号/无符号不匹配

我不知道这个警告到底在哪一行,但我假设它在这里:

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

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

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

比较a < b将会是假的。这是因为 a 被转换为无符号,并且由于负数的表示方式(通常是 two's complement )。但是,就您的情况而言,这不会成为问题。为了避免收到警告,请声明 iunsigned int .

  • 警告 C4033:“str_check”必须返回值
  • str_check的声明是:

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

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

  • 警告 C4101:“k”:未引用的局部变量
  • 这个很简单:你已经声明了一个变量 k而且你不使用它。

    <小时/>

    其余部分与上面重复(除了最后一个)。除了第二个警告(这可能会导致您出现一些意外行为)之外,程序应该可以完美运行。

    最后一个错误是你的问题。我将尽力向您提供一些有关该错误含义的背景知识以及一些引用资料,希望对您有所帮助。

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

    1. 预处理。

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

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

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

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

    关于c - 在 C 中删除另一个字符串中出现的字符串(代码和错误消息),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26823707/

    相关文章:

    c - 智能类型定义

    无法打开/开发/控制台

    c - 重复从 Fortran 调用 C 函数时出现段错误

    r - 计算R中的一组变量中值的出现次数(每行)

    python - 打印字符串中多次出现的情况

    c - 二维数组和动态分配如何工作?

    c - 在几个 header 中重新定义 'struct timeval'

    ios - 如何计算具有不同条件的 Swift 对象数组中某个元素的出现次数

    python - 如何在 Python 中找到字符的开始和结束出现

    c++ - 查找整数数组中数字的最大出现次数