I'm making a word search program in C which takes the user input and then chooses one of the global arrays of words and uses that to generate the word search. It works when I just use one of the global arrays but not with the choice, I want to copy the contents of the category arrays, depending on the users choice into the newArray which can be used with the word search. The program crashes after entering an option at the moment, here's what I have.
switch(choice)
{
case 1: choseArray(newArray, massEffect);
break;
case 2: choseArray(newArray, fallout3);
break;
case 3: choseArray(newArray, elderScrolls);
break;
case 4: choseArray(newArray, gameOfThrones);
break;
case 5: choseArray(newArray, breakingBad);
break;
default: printf("Enter a valid option!");
}
void choseArray(char** newArray, char** category)
{
int i;
for(i=0;i<6;i++)
{
strcpy(newArray[i], category[i]);
}
}
The arrays look like this and are declared globally for now
char gameOfThrones[6][250] = {"KINGSLANDING", "TYRIAN", "STARK", "LANISTERS", "WESTEROS", "WINTERFELL"};
char breakingBad[6][250] = {"JESSE", "WALT", "HEISENBERG", "SAUL", "GUSTAVO", "BREAKFAST"};
char newArray[6][250];
Didn't you get a compiler warning? Try declaring the function to match the arguments passed.
#include <stdio.h>
#include <string.h>
void choseArray(char newArray[][250], char category[][250]){
int i;
for(i=0;i<6;i++)
strcpy(newArray[i], category[i]);
}
int main()
{
char gameOfThrones[6][250] = {"KINGSLANDING", "TYRIAN", "STARK", "LANISTERS", "WESTEROS", "WINTERFELL"};
char breakingBad[6][250] = {"JESSE", "WALT", "HEISENBERG", "SAUL", "GUSTAVO", "BREAKFAST"};
char newArray[6][250];
choseArray(newArray, gameOfThrones);
return 0;
}
The way you were doing it
char **newArray
is a pointer to a pointer, or, an array of pointers.