What's wrong with this code? I am trying to use gcc to compile.
Code:#include <stdio.h> void testfun(buffer) { printf("%s\n", buffer); } int main(int argc char **argv) { char buf[2]; buf[0] = 'a'; buf[1] = '\0'; testfun(buf); return 0; }
What's wrong with this code? I am trying to use gcc to compile.
Code:#include <stdio.h> void testfun(buffer) { printf("%s\n", buffer); } int main(int argc char **argv) { char buf[2]; buf[0] = 'a'; buf[1] = '\0'; testfun(buf); return 0; }
Declare the parameter to testfun as
Otherwise, the compiler doesn't know what type it is.Code:char *buffer
Oh, and you're missing a comma between argc and argv in main().
Also, you can set buf more concisely with
Code:char buf[] = "a";
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Thanks for the pointers dwks, now why can't I compile this? Isn't it the same thing?
I get the following error message from gcc.Code:#include <stdio.h> void testfun(char *buffer); int main(int argc, char **argv) { char buf[2]; buf[0] = 'a'; buf[1] = '\0'; testfun(buf); return 0; } void testfun(buffer); { printf("%s\n", buffer); }
gcc -c main.c -I/usr/local/include
main.c:13: warning: parameter names (without types) in function declaration
main.c:13: error: syntax error before '{' token
*** Error code 1
Because you forgot to fix this parameter.Code:void testfun(buffer); {
buffer will default to int if you fail to specify it's type,
you get the error because of your semi-colon,
I assume buffer shouldn't be int anyway?Code:void testfun(buffer); {
Code:void testfun(char * buffer) {
argh it's been a rough day, thanks for the help.
Never omit the data type in any parameter, declaration OR definition.
Commas ( ; ) are only for declarations, not definitions of functions.