Thread: Illegal initialization while trying to compare two groups of integers using pointers

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    6

    Illegal initialization while trying to compare two groups of integers using pointers

    Hello everyone. Well, being a total n00b, I know I pretty much suck at this for now while I'm learning. I'm trying to compare two groups of integers and count the number of matches between the groups. With the current code I'm getting an "illegal initialization" error on line 8. I'm not yet able to see exactly where I went wrong though. Can you guys help me out with it please.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    
    int main() 
    {
        int sysHunds,sysTens,sysOnes,usrHunds,usrTens,usrOnes;
        int * drawSet[3]={&sysHunds,&sysTens,&sysOnes};
        int * usrSet[3]={&usrHunds,&usrTens,&usrOnes}; 
        int ds, us, matches=0;
        
            for (ds=0;ds<3;ds++) for (us=0;us<3;us++) 
            if (*drawSet[ds]==*usrSet[us]) matches++;
            printf("\nThere are %d matching number(s)!\n", matches);
            
        
    system("pause");
    return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I think you mean line 9 rather than line 8. Basically, prior to C99 (according to my compiler), you are not permitted to initialise an array like that, i.e., by taking the addresses of variables. If feasible, a simple solution is just to compile as C99, otherwise you would change to write:
    Code:
    int *drawSet[3];
    drawSet[0] = &sysHunds;
    /* etc */
    I recommend having two arrays of ints instead:
    Code:
    int drawSet[3];
    int usrSet[3];
    If you want to refer to different elements by name, use an enum for the indices.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    6
    Thanks for the help @laserlight. I appreciate it and I went with your first option (although I may change it to the arrays later) and it's almost perfect. The only problem now is that when either *drawSet or *usrSet has a number where the same digit appears twice (ex: 454) and it appears once in the other set (ex: 348) it counts it as two matches. The goal is to count once for each cross matched pair, not counting the same occurrence of a digit for a second time. Can this be done with changes to the current code or might there be an easier way to accomplish it?

    Here's the code again with printf of the above stated results:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    
    int main() 
    {
        int sysHunds=4,sysTens=5,sysOnes=4,usrHunds=3,usrTens=4,usrOnes=8,ds,us,matches=0;
        int * drawSet[3]; int * usrSet[3]; 
        
        drawSet[0] = &sysHunds;
        drawSet[1] = &sysTens;
        drawSet[2] = &sysOnes;
        usrSet[0] = &usrHunds;
        usrSet[1] = &usrTens;
        usrSet[2] = &usrOnes;
        
        printf("\n*drawSet: %d%d%d        usrSet: %d%d%d\n", *drawSet[0], *drawSet[1], *drawSet[2], *usrSet[0], *usrSet[1], *usrSet[2]);
        
            for (ds=0;ds<3;ds++) for (us=0;us<3;us++) 
            if (*drawSet[ds]==*usrSet[us]) matches++;
            
            printf("\nThere are %d matching number(s)!\n", matches);
    
    
        
    system("pause");
    return 0;
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by WraithBug
    The only problem now is that when either *drawSet or *usrSet has a number where the same digit appears twice (ex: 454) and it appears once in the other set (ex: 348) it counts it as two matches. The goal is to count once for each cross matched pair, not counting the same occurrence of a digit for a second time. Can this be done with changes to the current code or might there be an easier way to accomplish it?
    One approach is to sort both arrays and then step through them in a single pass each.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2014
    Posts
    6
    Quote Originally Posted by laserlight View Post
    One approach is to sort both arrays and then step through them in a single pass each.
    Thanks again. I'll try to get that done and hopefully see what happens.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ISO c++ forbids comparisson btween pointers and integers
    By Cursius in forum C++ Programming
    Replies: 4
    Last Post: 01-10-2010, 03:57 PM
  2. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  3. pointers to array initialization
    By enggabhinandan in forum C Programming
    Replies: 4
    Last Post: 10-24-2006, 09:52 PM
  4. Illegal Initialization
    By cboard_member in forum C Programming
    Replies: 2
    Last Post: 08-04-2005, 09:42 AM
  5. Initialization of Char Pointers?
    By Kleid-0 in forum C Programming
    Replies: 14
    Last Post: 01-07-2005, 10:41 PM