Thread: Syntax error:

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    1

    Syntax error:

    Hi, anyone knows why the highlighted lines(red color) cause syntax errors?
    Please advise.



    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    //declare structure
    struct result
    {
            int num_a=0, num_b=0;
    };
    //declare function prototype 
    struct result count( char str[], int idx );
    
    int main()
    {
            char str[] = "acdefAbbA";
            struct result res = count( str, 0 );
            /*char *pa=res.num_a, *pb=res.num_b;*/
            
            printf( "num a = %d and num b = %d\n", res.num_a, res.num_b );
            // should output : "num_a = 1 and num_b = 2"
            
            
            return 0;
    }
    
    struct result count( char str[], int idx )
    {
            switch( str[idx] )
            {
                    struct result res;
                    case 'a':
                                    (res.num_a)++;
                                    idx++;
                                    return count( str[idx], idx);
                    case 'b':
                                    (res.num_b)++;
                                    idx++;
                                    return count( str[idx], idx);
                    case '\0':
                                    return res;
            }
    }
    Last edited by joe_geek; 03-22-2010 at 01:10 AM.

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Listen to your compiler, it will tell you what is wrong. Post the error messages you don't understand.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    4
    int num_a=0, num_b=0;


    In structure you have declare above variable with initialization . That is problem.
    This one causes error because C does not allow in-struct initialization .
    You should define the struct without initializations.
    return count( str[idx], idx);

    In above statement you are calling the function with character argument and integer argument.
    Actually you have defined the function with accepting the character array integer.
    You should call the function like this. count ( str, idx ) ;
    It will work fine.

    struct result count( char str[], int idx )
    accepts a character array and an integer.
    But you are passing a character ... str[idx].
    Last edited by pavun_cool; 03-22-2010 at 01:13 AM. Reason: adding some statements

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM