【问题标题】:Format specifies type 'char *', but the argument has type 'char **'格式指定类型'char *',但参数有类型'char **'
【发布时间】:2014-04-13 11:53:17
【问题描述】:

我尝试通过硬编码来打印输出,但我收到了一个错误,因为我给出的参数是 char** 类型,printf 中的格式是指定类型 char*。

还有四行我不明白的代码(请参阅下面代码中的代码 cmets),所以如果有人解释该代码块会非常有帮助。

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

void inputParsing(char *src, char *end, char *destU, char *destP) {
    int x = 0;
    for(; src != end; src++){
        if((*src != '+') && x==0) {
            *destU = *src;
            destU++;
        }
        else if((*src != '+') && x==1){
            *destP = *src;
            destP++;
        }
        else {
            x = 1;
        }
    }
    *destU = ' ';                  //What does this line do?
    *destP = ' ';                  //What does this line do?
    *++destU = '0';                //What does this line do?
    *++destP = '0';                //What does this line do?
    printf("%s\n",&destU);
    printf("%s\n",&destP);
}

void inputStoring() {
    char inputArray[200];
    char usernameArray[200];
    char passwordArray[200];
    //int n = atoi(getenv("CONTENT_LENGTH"));
    //fgets(inputArray, n+1, stdin);
    strcpy(inputArray, "gaming+koko");
    int n = strlen(inputArray);
    inputParsing(inputArray, inputArray + n, usernameArray, passwordArray); //inputArray+n is referencing the array cell that contains the last inputted character.
}

int main(void) {
    inputStoring();
}

【问题讨论】:

  • 这段代码是从哪里来的?它的目的是什么? (不知道这一点,我们只能推测个别线路的目的......)
  • 我想你会对你标记的那些 after 的两行更感兴趣,因为它们是调用 undefined behavior 在你的程序中。标记的行只是使用指针取消引用运算符将​​单个字符存储在来自调用者的提供缓冲区中。 (使用第二对增加存储位置)。
  • @OliCharlesworth - 我编写这段代码是为了通过 POST 方法从网站获取输入。所以基本上我将输入存储在 inputArray 中,然后调用方法 inputParsing 将用户名和密码分开并将它们存储在两个不同的数组中。
  • 如果写了这段代码,你为什么要问我们这些行的目的是什么?
  • @WhozCraig - 是的,我理解标记线的作用,但我不明白为什么我需要这样做。我从教授的笔记中复制了那部分,所以我不确定为什么需要它。

标签: c cgi


【解决方案1】:

更正char* vs char**-issue 和'0' vs '\0'-issue 之后

*++destU = '\0';                //What does this line do?
*++destP = '\0';                //What does this line do?
printf("%s\n",destU);
printf("%s\n",destP);

代码仍然没有任何用处,因为destUdestP 将各自指向一个空字节,这意味着printf() 将命中'\0' 作为第一个字符,并且不会打印任何内容。 您可能应该解释一下代码应该做什么,这样我们就可以说出哪里出了问题。

【讨论】:

  • 谢谢,现在我明白了为什么它没有打印我想要它打印的东西。我现在可以修好其余的了。
【解决方案2】:

您的代码中的 destU 和 destP 是 char*。但是您在 printf 中使用 & 符号传递它们,这意味着您传递它们的地址,以便 printf 获得指向字符指针的地址。

你只需要将 destU 和 destP 传递给 printf 函数

printf("%s\n",destU);
printf("%s\n",destP);

这是我通过在 Google 上简单搜索找到的指针的小指南 http://pw1.netcom.com/~tjensen/ptr/pointers.htm

*destU = ' ';         /*Sets "space character" or ' ' at the current destU pointer position*/        
*destP = ' ';         /*Same but for destP*/                  
*++destU = '0';       /*Moves pointer position by 1 place forward and sets a value 0 byte in its place - this is not a string terminating value (\0 is) so u may have problems when trying to print the string thank you EOF for correcting*/
*++destP = '0';      /*Same but for destP*/

inputArray+n 也将是一个 \0 字节值,因此您可以询问该值是否为 \0(每个字符串都以 \0 结尾)

【讨论】:

  • '0' 不是空终止符。 '\0' 是。
  • 现在根据您发布的内容更改代码后,第一个 printf 不应该打印“游戏”,第二个打印“koko”吗?但是当我运行我的程序时,它不会打印任何东西。
  • 是的,因为 ='0' 你需要把 '\0' 放在那里。
  • @daniels_pa - 我也改变了它,但它只打印一个新的空白行,没有别的:/
  • 是的,因为您的 destU 和 destP 指针没有移动到开头,因此它们从当前位置开始在字符串中使用(这是空字节 - 因此字符串立即终止)。 U 需要保护 destU 和 destP 开头的指针,并在尝试打印它们之前将 destU 和 destP 指针设置为它们。添加代码 [code] char *save_dU; /*this在inputParsing方法的开头*/ char *save_dP; save_dU=destU; save_dP=destP; [/code] 和之前调用 printf [code] destU=save_dU; destP=save_dP; [/code]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-02
  • 2017-09-04
  • 1970-01-01
  • 2022-01-21
相关资源
最近更新 更多