0

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

#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.

任何的想法?

4

1 回答 1

4

GCC 支持嵌套函数,而clang 不支持(也不急于这样做)

看起来您的函数是偶然嵌套的;只需将其移出主要功能,它就会继续工作。

于 2012-08-09T07:53:02.803 回答