【问题标题】:Passing char pointer into a function error将 char 指针传递给函数错误
【发布时间】:2012-04-27 14:18:10
【问题描述】:

您好,当我尝试将 char 传递给函数时出现错误。 这是我的代码。

变量

char *temp;

原型

int checkIfUniqueCourseNo(char,int);

打电话

checkIfUniqueCourseNo(temp,k);

还有我的错误

warning: improper pointer/integer combination: arg #1

我是 C 新手,所以请放轻松 :)

【问题讨论】:

    标签: c function pointers char


    【解决方案1】:

    您的函数接受char;您正在尝试传递char*

    要解决此问题,您需要 dereference 指针以获取它指向的字符,以便您的函数接收它期望的参数类型:

    checkIfUniqueCourseNo(*temp,k);
    

    【讨论】:

    • @DanielDC - 在取消引用之前确保指针中有一个有效的地址(在你的问题中你刚刚声明了它,但没有初始化它)
    【解决方案2】:

    如果函数没有char,你应该取消引用指针:

    checkIfUniqueCourseNo(*temp,k);
    //                    ^ pass the char addressed by temp
    

    【讨论】:

      【解决方案3】:

      您的变量是char*(字符指针),但函数采用char(不是指针)。

      如果您想将 temp 的 内容 传递给函数,请使用 checkIfUniqueCourseNo(*temp, k)。如果您确实想传递指针本身,请将函数声明为

      int checkIfUniqueCourseNo(char*,int);
      

      【讨论】:

        猜你喜欢
        • 2021-09-07
        • 2016-03-27
        • 1970-01-01
        • 1970-01-01
        • 2011-05-22
        • 2018-09-16
        • 1970-01-01
        • 2013-12-06
        相关资源
        最近更新 更多