SIMEL
SIMEL

Reputation: 8941

syntax error before "char" in C

I have the following piece of code, which when I compile it, I get:

smash.c:22 error: syntax error before "char"

I don't understand where the problem is. (line 22 is marked by /*22*/ in the error message, but line numbers do not appear in the code). How can I correct this error?

/*some comments...*/
/*some more comments...*/

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>

#include "dir_handling.h"
#include "var_handling.h"



#define     MAXLENGTH           80        

void error_print (char* str)
{
    /*22*/ char *error_message=(*char)malloc((strlen(str)+strlen("smash error: > \"\"\n")+1)*sizeof(char));
    strcpy (error_message,"smash error: > \"");
    strcat(error_message,str);
    strcat(error_message,"\"\n");
    perror (error_message);
    free (error_message);
    // printf ("smash error: > \"%s\"\n",str);
}
...

Upvotes: 2

Views: 2292

Answers (2)

Mitch Wheat
Mitch Wheat

Reputation: 300579

It should be (char*), as in:

char *error_message=(char*)malloc( etc...

But note: it is good practice not to cast the return of malloc...

Upvotes: 3

Andrey Vlasovskikh
Andrey Vlasovskikh

Reputation: 16838

(*char) should be (char *).

Upvotes: 3

Related Questions