mko
mko

Reputation: 22044

Clang and gcc compiler gives my code different error and warning message/

I try to make a test code to get the pointer return value:

#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 compiles this code with no error and warning, but clang gives me 4 errors:

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.

any idea?

Upvotes: 0

Views: 201

Answers (1)

Jesper
Jesper

Reputation: 7605

GCC supports nested functions and clang does not (and isn't in a hurry to do so).

It looks like your function is nested by accident; just move it outside of the main function and it'll keep working.

Upvotes: 4

Related Questions