Copy array of strings into another array in C

Declan Fitzpatrick

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];
John Bollinger

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can't copy string to an array of strings in C

From Dev

Copy elements of one array to another in C

From Dev

Copy or Append first 13 index in Array of Strings to another and sort it - php

From Java

Copy array items into another array

From Dev

Copy byte array to another byte array in C#

From Dev

Byte array copy to another byte array in c#

From Dev

C#: Assign array to another array: copy or pointer exchange?

From Dev

Objective C copy array to another array with index 1

From Dev

How to copy contents in an array of object into another array in C++?

From Dev

Copy pointer array values to end of another array in C

From Dev

Copy char input into array of strings

From Dev

copy java array "over" another

From Dev

Error message if I try to copy one array into another in C

From Dev

copy a char array to another char array of arrays

From Dev

How to copy array value to another array

From Dev

Copy array key into another existing array key

From Dev

how to copy elements that are in an array but not in another on a third array

From Dev

how to copy the elements of an array to another array

From Dev

Use one array of strings to splice another array?

From Dev

Copy reference to array in C

From Dev

Using C Strings in an array

From Dev

Randomizing an array of C strings

From Dev

Grouping array of Strings C

From Dev

C storing strings into an array

From Dev

bsearch() on an array of strings in C

From Dev

C - Copying array of strings

From Dev

Copy a string into 2D array of strings

From Dev

Copy a string to a malloc'd array of strings

From Dev

How to copy array keys to another array, and replace values with empty array?

Related Related

HotTag

Archive