Simple problem

This is a discussion on Simple problem within the C Programming forums, part of the General Programming Boards category; What's wrong with this code? I am trying to use gcc to compile. Code: #include <stdio.h> void testfun(buffer) { printf("%s\n", ...

  1. #1
    tar
    tar is offline
    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,048
    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
    tar
    tar is offline
    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
    tar
    tar is offline
    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
    Posts
    23,021
    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-17-2010, 12:00 AM
  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, 08:37 PM
  5. Simple OO Problem
    By bstempi in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2004, 06:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21