4

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];
2
  • why not void choseArray(char newArray[6][250], .... ? Commented Apr 1, 2015 at 14:50
  • 1
    You have not presented the declaration of newArray that applies in the scope of the switch statement, which is of great relevance. Commented Apr 1, 2015 at 14:50

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

If you declare your word lists in this form ...

char gameOfThrones[6][250] = { ... };

then they are arrays of arrays of chars. The function parameter type char ** is a pointer to a char pointer, which is not even directly comparable. Your compiler should have thrown a fit about this.

Supposing that newArray is of the same type as the base word lists, you should declare your function like so:

void choseArray(char newArray[][250], char category[][250]) { ... }

... or possibly like so:

void choseArray(char (*newArray)[250], char (*category)[250]) { ... }

... to match the actual argument types. The body of your function probably works as-is in that case.

1 Comment

Thank you very much, got it working with that and I understand why now, i didn't get any compiler warning from devc++

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.