【问题标题】:Pointers in C not working as expectedC 中的指针未按预期工作
【发布时间】:2014-11-15 23:44:26
【问题描述】:

好的,这是本准则的重要部分。只是摘录。

在 steuer.c 中:

static char (*pointer)[8];

extern void test(char *pointer[]); // im not sure, whether this calling is correct

int main(void)
{
    int i;
    check = malloc(sizeof(*pointer) *10);
    for(i=0;i<100;i++)
        test(check[1]);
    ...
}

在compute.c中:

void test(char * compute[])
{
    char temp="test";
    if(strcmp(compute, temp) == 0)
        return 1;
    else
        return 0;
}

问题是:预期的 »char **« 但参数的类型是 »char *

谢谢

【问题讨论】:

  • 您发布的代码在使用 GCC 编译时存在许多不相关的语法错误。这是由于符号“main”之后缺少参数等问题造成的。你能确保你发布了正确的源代码吗?
  • 代码只是摘录。 Point 是从 main 到 function 测试的正确转移。
  • 代码只是一个摘录,这很好,但它根本无法编译。如果您正在寻求有关错误的帮助,则应提供专门导致该错误的示例或示例。例如,我收到有关未声明 check 的错误。我可以在我的副本中添加一个声明,但解决问题确实需要知道你是如何声明 check 的。同样,test(check[]); 是无效语法,因为[] 内没有任何内容。同样,如果没有这些信息,我无法解决您的问题。
  • 好的,我已经更新了代码。这是操作。
  • 请注意,您现在已从问题中删除了错误消息。对于任何试图回答这个问题的人,它是expected »char **« but argument is of type »char *«

标签: c arrays pointers char


【解决方案1】:

问题在于您已声明 test 接受 char*[] 参数 - 字符指针数组,也就是字符串数组。

在这两个文件中都清楚地表明您将其用作字符指针 - char* - 因此应该只是一个字符串。

您需要将void test(char * compute[]) 更改为void test(char *compute),以便类型正常工作。 (您还需要在其他声明中进行相同的更改。)

【讨论】:

    【解决方案2】:

    样本...

    static char (* pointer)[8];
    
    void test1(char * pointer){ ; }
    void test2(char (* pointer)[8]){ ; }
    
    int main(void){
        char (*check)[8] = malloc(sizeof(*pointer) *10);
    
        test1(&check[0][0]);
        test2(check);
    
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多