loading 2 character arrays in a row

This is a discussion on loading 2 character arrays in a row within the C Programming forums, part of the General Programming Boards category; I am having a strange problem. I am attempting to load two character arrays in a row. The program then ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    17

    loading 2 character arrays in a row

    I am having a strange problem. I am attempting to load two character arrays in a row. The program then sends one of the arrays to a function called binarysearch. The code looks like this:

    Code:
    strcpy (irregverbs[0], "be");
    strcpy (irregverbs[1], "bear");
    strcpy	(	irregverbs [2]	,	"beat"	)	;
    strcpy	(	irregverbs [3]	,	"become"	)	;
    strcpy	(	irregverbs [4]	,	"begin"	)	;
    strcpy	(	irregverbs [5]	,	"bend"	)	;
    strcpy	(	irregverbs [6]	,	"bite"	)	;
    strcpy	(	irregverbs [7]	,	"blow"	)	;
    strcpy	(	irregverbs [8]	,	"break"	)	;
    strcpy	(	irregverbs [9]	,	"bring"	)	;
    strcpy	(	irregverbs [10]	,	"build"	)	;
    strcpy	(	irregverbs [11]	,	"buy"	)	;
    strcpy	(	irregverbs [12]	,	"catch"	)	;
    strcpy	(	irregverbs [13]	,	"choose"	)	;
    strcpy	(	irregverbs [14]	,	"cling"	)	;
    strcpy	(	irregverbs [15]	,	"come"	)	;
    strcpy	(	irregverbs [16]	,	"cut"	)	;
    strcpy	(	irregverbs [17]	,	"dig"	)	;
    strcpy	(	irregverbs [18]	,	"do"	)	;
    strcpy	(	irregverbs [19]	,	"draw"	)	;
    strcpy	(	irregverbs [20]	,	"drink"	)	;
    strcpy	(	irregverbs [21]	,	"eat"	)	;
    strcpy	(	irregverbs [22]	,	"fall"	)	;
    strcpy	(	irregverbs [23]	,	"feed"	)	;
    strcpy	(	irregverbs [24]	,	"feel"	)	;
    strcpy	(	irregverbs [25]	,	"find"	)	;
    strcpy	(	irregverbs [26]	,	"flee"	)	;
    strcpy	(	irregverbs [27]	,	"fly"	)	;
    strcpy	(	irregverbs [28]	,	"forget"	)	;
    strcpy	(	irregverbs [29]	,	"forgive"	)	;
    strcpy	(	irregverbs [30]	,	"get"	)	;
    strcpy	(	irregverbs [31]	,	"give"	)	;
    strcpy	(	irregverbs [32]	,	"go"	)	;
    strcpy	(	irregverbs [33]	,	"grind"	)	;
    strcpy	(	irregverbs [34]	,	"grow"	)	;
    strcpy	(	irregverbs [35]	,	"hang"	)	;
    strcpy	(	irregverbs [36]	,	"have"	)	;
    strcpy	(	irregverbs [37]	,	"hear"	)	;
    strcpy	(	irregverbs [38]	,	"hide"	)	;
    strcpy	(	irregverbs [39]	,	"hit"	)	;
    strcpy	(	irregverbs [40]	,	"hold"	)	;
    strcpy	(	irregverbs [41]	,	"hurt"	)	;
    strcpy	(	irregverbs [42]	,	"keep"	)	;
    strcpy	(	irregverbs [43]	,	"know"	)	;
    strcpy	(	irregverbs [44]	,	"lead"	)	;
    strcpy	(	irregverbs [45]	,	"leave"	)	;
    strcpy	(	irregverbs [46]	,	"let"	)	;
    strcpy	(	irregverbs [47]	,	"light"	)	;
    strcpy	(	irregverbs [48]	,	"lose"	)	;
    strcpy	(	irregverbs [49]	,	"make"	)	;
    strcpy	(	irregverbs [50]	,	"meet"	)	;
    strcpy	(	irregverbs [51]	,	"pay"	)	;
    strcpy	(	irregverbs [52]	,	"put"	)	;
    strcpy	(	irregverbs [53]	,	"read"	)	;
    strcpy	(	irregverbs [54]	,	"ride"	)	;
    strcpy	(	irregverbs [55]	,	"ring"	)	;
    strcpy	(	irregverbs [56]	,	"rise"	)	;
    strcpy	(	irregverbs [57]	,	"run"	)	;
    strcpy	(	irregverbs [58]	,	"say"	)	;
    strcpy	(	irregverbs [59]	,	"see"	)	;
    strcpy	(	irregverbs [60]	,	"seek"	)	;
    strcpy	(	irregverbs [61]	,	"sell"	)	;
    strcpy	(	irregverbs [62]	,	"send"	)	;
    strcpy	(	irregverbs [63]	,	"set"	)	;
    strcpy	(	irregverbs [64]	,	"shake"	)	;
    strcpy	(	irregverbs [65]	,	"shoot"	)	;
    strcpy	(	irregverbs [66]	,	"sing"	)	;
    strcpy	(	irregverbs [67]	,	"sink"	)	;
    strcpy	(	irregverbs [68]	,	"sit"	)	;
    strcpy	(	irregverbs [69]	,	"sleep"	)	;
    strcpy	(	irregverbs [70]	,	"speak"	)	;
    strcpy	(	irregverbs [71]	,	"split"	)	;
    strcpy	(	irregverbs [72]	,	"spread"	)	;
    strcpy	(	irregverbs [73]	,	"stand"	)	;
    strcpy	(	irregverbs [74]	,	"steal"	)	;
    strcpy	(	irregverbs [75]	,	"stick"	)	;
    strcpy	(	irregverbs [76]	,	"strike"	)	;
    strcpy	(	irregverbs [77]	,	"swear"	)	;
    strcpy	(	irregverbs [78]	,	"swim"	)	;
    strcpy	(	irregverbs [79]	,	"take"	)	;
    strcpy	(	irregverbs [80]	,	"teach"	)	;
    strcpy	(	irregverbs [81]	,	"tear"	)	;
    strcpy	(	irregverbs [82]	,	"tell"	)	;
    strcpy	(	irregverbs [83]	,	"think"	)	;
    strcpy	(	irregverbs [84]	,	"throw"	)	;
    strcpy	(	irregverbs [85]	,	"wake"	)	;
    strcpy	(	irregverbs [86]	,	"wear"	)	;
    strcpy	(	irregverbs [87]	,	"win"	)	;
    strcpy	(	irregverbs [88]	,	"write"	)	;
    
    strcpy (irregverbspast[0], "was");
    strcpy (irregverbspast[1], "bore");
    strcpy	(	irregverbspast [2]	,	"beat"	)	;
    strcpy	(	irregverbspast [3]	,	"became"	)	;
    strcpy	(	irregverbspast [4]	,	"began"	)	;
    strcpy	(	irregverbspast [5]	,	"bent"	)	;
    strcpy	(	irregverbspast [6]	,	"bit"	)	;
    strcpy	(	irregverbspast [7]	,	"blew"	)	;
    strcpy	(	irregverbspast [8]	,	"broke"	)	;
    strcpy	(	irregverbspast [9]	,	"brought"	)	;
    strcpy	(	irregverbspast [10]	,	"built"	)	;
    strcpy	(	irregverbspast [11]	,	"bought"	)	;
    strcpy	(	irregverbspast [12]	,	"caught"	)	;
    strcpy	(	irregverbspast [13]	,	"chose"	)	;
    strcpy	(	irregverbspast [14]	,	"clung"	)	;
    strcpy	(	irregverbspast [15]	,	"came"	)	;
    strcpy	(	irregverbspast [16]	,	"cut"	)	;
    strcpy	(	irregverbspast [17]	,	"dug"	)	;
    strcpy	(	irregverbspast [18]	,	"did"	)	;
    strcpy	(	irregverbspast [19]	,	"drew"	)	;
    strcpy	(	irregverbspast [20]	,	"drank"	)	;
    strcpy	(	irregverbspast [21]	,	"ate"	)	;
    strcpy	(	irregverbspast [22]	,	"fell"	)	;
    strcpy	(	irregverbspast [23]	,	"fed"	)	;
    strcpy	(	irregverbspast [24]	,	"felt"	)	;
    strcpy	(	irregverbspast [25]	,	"found"	)	;
    strcpy	(	irregverbspast [26]	,	"fled"	)	;
    strcpy	(	irregverbspast [27]	,	"flew"	)	;
    strcpy	(	irregverbspast [28]	,	"forgot"	)	;
    strcpy	(	irregverbspast [29]	,	"forgave"	)	;
    strcpy	(	irregverbspast [30]	,	"got"	)	;
    strcpy	(	irregverbspast [31]	,	"gave"	)	;
    strcpy	(	irregverbspast [32]	,	"went"	)	;
    strcpy	(	irregverbspast [33]	,	"ground"	)	;
    strcpy	(	irregverbspast [34]	,	"grew"	)	;
    strcpy	(	irregverbspast [35]	,	"hung"	)	;
    strcpy	(	irregverbspast [36]	,	"had"	)	;
    strcpy	(	irregverbspast [37]	,	"heard"	)	;
    strcpy	(	irregverbspast [38]	,	"hid"	)	;
    strcpy	(	irregverbspast [39]	,	"hit"	)	;
    strcpy	(	irregverbspast [40]	,	"held"	)	;
    strcpy	(	irregverbspast [41]	,	"hurt"	)	;
    strcpy	(	irregverbspast [42]	,	"kept"	)	;
    strcpy	(	irregverbspast [43]	,	"knew"	)	;
    strcpy	(	irregverbspast [44]	,	"led"	)	;
    strcpy	(	irregverbspast [45]	,	"left"	)	;
    strcpy	(	irregverbspast [46]	,	"let"	)	;
    strcpy	(	irregverbspast [47]	,	"lit"	)	;
    strcpy	(	irregverbspast [48]	,	"lost"	)	;
    strcpy	(	irregverbspast [49]	,	"made"	)	;
    strcpy	(	irregverbspast [50]	,	"met"	)	;
    strcpy	(	irregverbspast [51]	,	"paid"	)	;
    strcpy	(	irregverbspast [52]	,	"put"	)	;
    strcpy	(	irregverbspast [53]	,	"read"	)	;
    strcpy	(	irregverbspast [54]	,	"rode"	)	;
    strcpy	(	irregverbspast [55]	,	"rang"	)	;
    strcpy	(	irregverbspast [56]	,	"rose"	)	;
    strcpy	(	irregverbspast [57]	,	"ran"	)	;
    strcpy	(	irregverbspast [58]	,	"said"	)	;
    strcpy	(	irregverbspast [59]	,	"saw"	)	;
    strcpy	(	irregverbspast [60]	,	"sought"	)	;
    strcpy	(	irregverbspast [61]	,	"sold"	)	;
    strcpy	(	irregverbspast [62]	,	"sent"	)	;
    strcpy	(	irregverbspast [63]	,	"set"	)	;
    strcpy	(	irregverbspast [64]	,	"shook"	)	;
    strcpy	(	irregverbspast [65]	,	"shot"	)	;
    strcpy	(	irregverbspast [66]	,	"sung"	)	;
    strcpy	(	irregverbspast [67]	,	"sank"	)	;
    strcpy	(	irregverbspast [68]	,	"sat"	)	;
    strcpy	(	irregverbspast [69]	,	"slept"	)	;
    strcpy	(	irregverbspast [70]	,	"spoke"	)	;
    strcpy	(	irregverbspast [71]	,	"split"	)	;
    strcpy	(	irregverbspast [72]	,	"spread"	)	;
    strcpy	(	irregverbspast [73]	,	"stood"	)	;
    strcpy	(	irregverbspast [74]	,	"stole"	)	;
    strcpy	(	irregverbspast [75]	,	"stuck"	)	;
    strcpy	(	irregverbspast [76]	,	"struck"	)	;
    strcpy	(	irregverbspast [77]	,	"swore"	)	;
    strcpy	(	irregverbspast [78]	,	"swam"	)	;
    strcpy	(	irregverbspast [79]	,	"took"	)	;
    strcpy	(	irregverbspast [80]	,	"taught"	)	;
    strcpy	(	irregverbspast [81]	,	"tore"	)	;
    strcpy	(	irregverbspast [82]	,	"told"	)	;
    strcpy	(	irregverbspast [83]	,	"thought"	)	;
    strcpy	(	irregverbspast [84]	,	"threw"	)	;
    strcpy	(	irregverbspast [85]	,	"woke"	)	;
    strcpy	(	irregverbspast [86]	,	"wore"	)	;
    strcpy	(	irregverbspast [87]	,	"won"	)	;
    strcpy	(	irregverbspast [88]	,	"wrote"	)	;
    
    
    
    printf ("\n\n%s %s\n\n", irregverbs[1], irregverbspast[1]);
    
    
                    for (j= 0; j <verbcount+1; j=j+1) {
                    location1=binarysearch (irregverbs, numberofirregverbs, verb, j);
                    printf ("\ni ran\n");
                    if (location1==0)
                    printf ("\nyou found an irregular past tense verb: %s\n\n", verb [j]);
                    }
    }
    The function binarysearch looks like this:

    Code:
    int binarysearch(char data[][94], int n, char newitem[][21], int positionofnewitem)
    
    
    
        {
            int low, high, test, findnewitem;
            low=0;
            high=n-1;
    
            while (low<=high){
              test=(low +high)/2;
    
    
                findnewitem=strcmp (newitem[positionofnewitem], data[test]);
                printf ("\ni am comparing %s to %s\n", newitem[positionofnewitem], data[test]);
    
    
                if(findnewitem==0)
                return findnewitem;
                else if (findnewitem<0)
                high = test -1;
                else
                low = test+1;
            }
            return -1;
        }
    It seems that for some reason the loading of the second array interferes with the values in the first array. Right after both arrays are loaded, i have a print statement asking that the value in position 1 of each array be printed to the screen. The output that i got for that was "bought bore" The output that I expected was bear bore. ALso, when i send irregverbs to the function, it seems to use the data from irregverbspast. As a matter of fact, when I took out the code that loads the irregpast array and just left in the values for irregpast, the program ran well, and the correct values were sent to the function..I can't make sense out of this....Anyone have any ideas as to what might be going wrong here??
    Last edited by Salem; 12-09-2010 at 09:26 AM. Reason: Fix the tags!

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You didn't show us were you declared these arrays... Might be a hint there...

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    17
    Arrays were declared as such:

    Code:
    char irregverbs [10][94];
    char irregverbspast [10][94];

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So if irregverbs has 10 spots in it, why are you trying to write to irregverbs[88]? (HINT: 88 > 10.)

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by rivkyfried1 View Post
    Arrays were declared as such:

    Code:
    char irregverbs [10][94];
    char irregverbspast [10][94];
    As tabstop pointed out this defines 10 lines of 94 characters.
    You have your array bounds reversed...

    char irregularverbs[94][10];
    char irregverbspast[94][10];

    ... gives you 94 strings of 9 characters each... i.e. 9 plus the trailing null.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 10-07-2008, 07:19 PM
  2. Character Arrays
    By danielakkerman in forum C Programming
    Replies: 5
    Last Post: 05-20-2007, 05:48 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Comparing Character Arrays
    By LostNotFound in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2003, 12:42 PM
  5. Array of Character Arrays
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 02-09-2002, 06:07 PM

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