tags:

views:

64

answers:

4

I've prepared this simple example which is not working for me

#include <stdio.h>
#include <stdlib.h>

FILE *fp;
char filename[] = "damy.txt";

void echo (char[] text)
{
    fp = fopen(filename, "a");
    fwrite(text, 1, strlen(text), fp);
    fclose(fp);
    printf(text);
}

int main ()
{
    echo("foo bar");
    return 0;
}

It's supposed to write both to command window and to file. However, this gives compilation error - the text used in echo() is not declared. Does c need another declaration of the variable?

A: 

I'm pretty sure you're supposed to do "char *" not "char []"

barrycarter
A string is not an array of char!
barrycarter
+5  A: 

Use char text[] or char* text, not char[] text.

Julien Lebosquain
in the parameter list to the function
Kate Gregory
+2  A: 

The line:

void echo (char [] text )

should be:

void echo (char text [])

And you need:

#include <string.h>

to get the declaration of the strlen function.

anon
A: 

Clearly the syntax

char[] var;

is a javaism. In C it should be char var[]. Moreover you must add #include <string.h>

ShinTakezou