将指针传递给函数的问题

Issues with Passing Pointers to Function

我正在尝试编写一些东西,它将采用两个命令行参数(一个单词和一个字母),并从单词中删除该字母的所有实例。例如,如果我传递 Hello 和 e,Hllo 应该是结果字符串。

我正在尝试使用指针来执行此操作,并减少不必要的局部变量以提高效率。编译时我收到明显的不兼容警告,我知道这与我的指针有关。如果我使用局部变量并传递它们,一切都会完美无缺 - 所以我对指针的理解显然有缺陷。

任何指点(无双关语意)将不胜感激!

// Function Prototypes
void remove_all_chars(char *, char *);

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[]) { //Function parameters allow for CLI arguments

    printf("%s\n", argv[1]);
    printf("%s\n", argv[2]);
    char *charTest2 = *argv[2];
    char *strTest = strdup(argv[1]);
    //char strTest[20];
    //char charTest[20];
    //strncpy(strTest, argv[1], 10);
    //strTest[0] = *argv[1];
    remove_all_chars(strTest, charTest2);
    printf("%s", strTest);

    return 0;

}


void remove_all_chars(char *str, char *c) {
    char *pr = str, *pw = str;
    while (*pr) {
        *pw = *pr++;
        pw += (*pw != c);
    }
    *pw = '[=10=]';
}

编辑:将 char* 添加到我的数据类型后,我得到以下警告:

Removing3.c:12:8: warning: incompatible integer to pointer conversion initializing 'char *' with an
      expression of type 'char'; remove * [-Wint-conversion]
        char *charTest2 = *argv[2];
              ^           ~~~~~~~~
Removing3.c:30:20: warning: comparison between pointer and integer ('int' and 'char *')
        pw += (*pw != c);
               ~~~ ^  ~
2 warnings generated.

编辑:这是警告

Removing3.c:18:19: warning: incompatible integer to pointer conversion passing 'char' to parameter of
      type 'char *'; take the address with & [-Wint-conversion]
        remove_all_chars(strTest, charTest2);
                         ^~~~~~~
                         &
Removing3.c:2:29: note: passing argument to parameter here
void remove_all_chars(char *, char *);
                            ^
Removing3.c:18:28: warning: incompatible integer to pointer conversion passing 'char' to parameter of
      type 'char *'; take the address with & [-Wint-conversion]
        remove_all_chars(strTest, charTest2);
                                  ^~~~~~~~~
                                  &
Removing3.c:2:37: note: passing argument to parameter here
void remove_all_chars(char *, char *);
                                    ^
Removing3.c:19:15: warning: format specifies type 'char *' but the argument has type 'char' [-Wformat]
        printf("%s", strTest);
                ~~   ^~~~~~~
                %c
Removing3.c:30:20: warning: comparison between pointer and integer ('int' and 'char *')
        pw += (*pw != c);
               ~~~ ^  ~
4 warnings generated.

您的函数采用 char* 类型的参数,但您的局部变量 char charTest2 = *argv[2]; char strTest = *argv[1]char 类型的。 char 类型的变量能够保存一个字符,然后您的变量将保存相应 argv 的第一个字母。相比之下,char * 是指向内存中字符的指针;它通常指向一系列字符的第一个,然后被视为 "string";序列(即字符串)以字符串终止字符 '[=20=]' 结尾,即 0-value.

首先,将它们改为char*;第二:我会避免直接更改 argv 中的字符串,而是复制一份:

char *strTest = strdup(argv[1]); 
char *charTest2 = argv[2]; // will not be changed; no copy necessary

如果strdup不可用,你可以这样写:

char *strTest = malloc(strlen(argv[1])+1);
strcpy(strTest,argv[1]);

你需要改变

    pw += (*pw != c);

进入

    pw += (*pw != *c);