Why does it show that my function returns an int when it returns a char*?

Shehzaad

The yacc code:

    %{
            #include<stdio.h>
        #include<string.h>



    %}

    %union {
                    char* dval;

            }

    %token <dval> NUM VAR
    %type <dval> E P
    %left '+' '-'
    %left '*' '/'

    %%
    statement : P {printf("\nt = %s\n \n",$1);}
              ;
    P: E
       ;

    E : E '+' E {strcpy($$,gencode($1,"+",$3));}
      | E '-' E {strcpy($$,gencode($1,"-",$3));}
      | E '*' E {strcpy($$,gencode($1,"*",$3));}
      | E '/' E {strcpy($$,gencode($1,"/",$3));}
      | '(' E ')' {strcpy($$,$2);}
      | NUM {strcpy($$,$1);}
      | VAR {strcpy($$,$1);}
      ;
    %%

**The lex code:**


   %{
            #include<stdio.h>
        #include<stdlib.h>
        #include<string.h>
            #include"y.tab.h"
        int n=0;
        char *ch="t";


    %}
    %%
    [0-9]+ {strcpy(yylval.dval,yytext); return NUM;}
    [a-z]+ {strcpy(yylval.dval,yytext); return VAR;}
    \n {return 0;}
    . {return yytext[0];}
    %%
    void yyerror(char* str)
    {
            printf("\n%s",str);
    }

    char* gencode(char *first,char *op,char *second)
    {
        char  *t;
        char x[5];
        t=(char*) malloc(sizeof(char)*5);

        strcpy(t, ch);
        itoa(n, x);
        strcat(t, x);        
        printf("\n%s = %s %s %s\n",t,first,op,second);
        n++;
        t[strlen(t)]='\0';

        return t;
    }

    main()
    {
            yyparse();
            return 0;
    }

For some reason gcc outputs the error: warning: passing argument 2 of ‘strcpy’ makes pointer from integer without a cast [enabled by default]. Where as the second argument of strcpy is the function gencode which returns a char* and not an integer.

rici

You need to declare gencode in the beginning of the yacc file. Otherwise, the compiler sees it as undefined and assumes it returns an int.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why does the Promise constructor require a function that calls 'resolve' when complete, but 'then' does not - it returns a value instead?

Why strcmp returns int but not char?

Why does this definition returns a function?

Why does my function composition implemented by reduce returns a closure?

why does my async function returns undefine while working with mysql2?

Why my async function returns an undefined when i try to access it?

why does groupby function returns duplicated data

Why my debounce function returns undefined value?

Why does printing reference of a char type variable returns the value in it?

Why is the copy constructor not called when the function returns?

Why does Convert.ToInt32 of a char returns the ascii code?

Why my SQL Function returns unexpected value?

Why does this function returns '' instead of a concatenated string

Why does my function always returns false, even if it finds return true before?

Why does printing a function name returns a value?

method that returns char with int

Why Does Hex() Function returns a string instead an int hex?

Why does my VBA function not evaluate the source cell that returns a HYPERLINK based on an IF statement

Why does my function only returns the first object from the array

Why Does Using the Reference Operator(&) After my Function Type Allow me to Modify the Value it Returns?

Why does my async function that returns a Future<int> stall at await for each?

why function returns undefined when it should be 'n/a'?

why does my SQL execute(select exists) function always returns 1?

Why does this query function returns incorrect results?

Why does Remix say int256 returned when my function returns int?

Why does standard golang big.Int function use the receiver if it takes two arguments and returns a value?

Why TypeScript compiler does not give error when function returns undefined while function's return type is number?

Why my function returns int without sum of 1, or it sums random number

Why won't my error message show when a filter returns empty