2015-04-02 40 views
0

我试图通过值传递一个数组,或者更确切地说是一个指向函数BinaryToHex的数组的指针。但是,我始终得到错误“功能BinaryToHex相冲突的类型”。将数组传递给函数时出错

这是程序的相关部分。

char *ConvertCodeToHex(char code[16])  
{     
    char nibble[4]; 
    char hexvalue[4]; 
    int i;int j,k = 0;  

    for(i=0; code[i] != '\0'; i++)  
     { 
      if((i+5)%4 == 0)   
      { 
       nibble[j] = '\0'; 
       j = 0;    
       hexvalue[k] = BinaryToHex(nibble); 
       k++; 
      } 
      nibble[j] = code[i]; 
      j++; 
     } 
    strncpy(finalhex, hexvalue, 4); //finalhex is a global character array 
    return finalhex; 
} 
char BinaryToHex(char b[4])  //The error is caught in this line of code. 
{ 
    int temp = 0,i; char buffer; 
    for(i=4; i >= 0; i--) 
     { 
      int k = b[i]-='0'; 
      temp += k*pow(2,i); 
     } 
//Converting decimal to hex. 
    if (temp == 10) 
     return 'A'; 
    else if (temp == 11) 
     return 'B'; 
    else if (temp == 12) 
     return 'C'; 
    else if (temp == 13) 
     return 'D'; 
    else if (temp == 14) 
     return 'E'; 
    else if (temp == 15) 
     return 'F'; 
    else 
     return (char)(((int)'0')+ temp); 

} 
+0

可能重复的http://stackoverflow.com/questions/1549631/getting-conflicting-types-for-function-in-c-why – Downvoter 2015-04-02 07:58:11

回答

0

在调用它之前,您需要函数声明,因此在顶部添加额外的行,例如

char BinaryToHex(char b[4]); 
char *ConvertCodeToHex(char code[16])  
{     
    char nibble[4]; 
    char hexvalue[4]; 
    int i;int j,k = 0;  

    for(i=0; code[i] != '\0'; i++)  
     { 
      if((i+5)%4 == 0)   
      { 
       nibble[j] = '\0'; 
       j = 0;    
       hexvalue[k] = BinaryToHex(nibble); 
       k++; 
      } 
      nibble[j] = code[i]; 
      j++; 
     } 
    strncpy(finalhex, hexvalue, 4); //finalhex is a global character array 
    return finalhex; 
}  
+0

谢谢!这为我节省了很多时间。 – 2015-04-02 08:13:37

0

您需要在ConvertCodeToHex()之前添加函数前向声明char BinaryToHex(char b[4]);。否则,当从ConvertCodeToHex()调用BinaryToHex()时,您的编译器将不知道函数描述。

+0

谢谢你的回答:) – 2015-04-02 08:13:22

0

您不需要在函数定义中传递数组索引。只需简单地写char BinaryToHex(char b [])。看看它会工作。

+0

将它作为char b [4]传递也是可行的。这不是错误所在。 – 2015-04-02 08:14:44