Thread: Simple problem

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    12

    Simple problem

    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;
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Declare the parameter to testfun as
    Code:
    char *buffer
    Otherwise, the compiler doesn't know what type it is.

    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.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    12
    Thanks for the pointers dwks, now why can't I compile this? Isn't it the same thing?

    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);
    }
    I get the following error message from gcc.

    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

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    void testfun(buffer); {
    Because you forgot to fix this parameter.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    buffer will default to int if you fail to specify it's type,

    you get the error because of your semi-colon,
    Code:
    void testfun(buffer); {
    I assume buffer shouldn't be int anyway?
    Code:
    void testfun(char * buffer)
    {

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    12
    argh it's been a rough day, thanks for the help.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Never omit the data type in any parameter, declaration OR definition.
    Commas ( ; ) are only for declarations, not definitions of functions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple file I/O problem
    By eecoder in forum C Programming
    Replies: 10
    Last Post: 10-16-2010, 11:00 PM
  2. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 02:45 AM
  3. Problem in very simple code
    By richdb in forum C Programming
    Replies: 22
    Last Post: 01-14-2006, 09:10 PM
  4. Simple Initialization Problem
    By PsyK in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2004, 07:37 PM
  5. Simple OO Problem
    By bstempi in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2004, 05:33 PM