Clang和gcc编译器给出了我的代码不同的错误和警告信息/

时间:2012-08-09 07:36:20

标签: pointers gcc clang

我尝试制作测试代码以获取指针返回值:

#include <stdio.h>

int main(void){

  char myStrcpy(char *str1,char *str2){
    while(*str2 != '\0'){
      *str1++ = *str2++;
    }
    *str1 = '\0';
    return str1;// return the final pointer that should point to the '\0'
  }

  char *reValue;
  char string1[] = "abcd";
  char string2[10];
  reValue = myStrcpy(string2,string1);
  reValue--;//now it should point to the last character which is `d`
  printf("this value of string 2 is %s\n",string2);
  printf("the return value the function is %c\n",*reValue);
  return 0;
}

gcc编译此代码时没有错误和警告,但是clang给了我4个错误:

my_stcpy.c:4:40: error: expected ';' at end of declaration
        char*  myStrcpy(char *str1,char *str2){
                                              ^
                                              ;
my_stcpy.c:14:2: error: use of undeclared identifier 'reValue'
        reValue = myStrcpy(string2,string1);
        ^
my_stcpy.c:15:2: error: use of undeclared identifier 'reValue'
        reValue--;
        ^
my_stcpy.c:17:50: error: use of undeclared identifier 'reValue'
        printf("the return value the function is %c\n",*reValue);
                                                        ^
4 errors generated.

任何想法?

1 个答案:

答案 0 :(得分:4)

GCC支持嵌套函数和clang does not (and isn't in a hurry to do so)

看起来你的功能是偶然嵌套的;只需将它移到主要功能之外,它就会继续工作。