简体   繁体   中英

Putting arrays into header files in Dev-cpp

This is my header file, which basically generates a random word from the list, and then finds the length of the word, and then sends the information back to the main code.

int WordDevelopment()
{    
char words[10][15] =
{
    "seanna",//6        //0
    "yellow",//6        //1
    "marshmallow",//11  //2
    "potato",//6        //3
    "beach",//5         //4
    "icecream",//8      //5
    "seven",//5         //6
    "giraffe",//7       //7
    "random",//6        //8
    "xylophone",//9     //9
};

//Generates random word
srand (time(NULL));
int randomIndex = rand() % 10;
int x = 0;
int a;
int randvar = 0;
randvar = rand();
randomIndex = randvar % 10;
printf("Row: %d\n",randomIndex);

//Stores the whole word in the word array
char word[15];
for(a=0; a<15; a++)
{
    word[a]=words[randomIndex][a];
}
printf("word: %s\n",word);

//Finds the word length
int wordlength = strlen(words[randomIndex]);
printf("word length: %d\n",wordlength);
}

I would like to know how to properly put the array at the top into this header file, and be able to access the variables from the header file in my main code.

The header files are meant to contains declaration of variable and function prototypes that are used by different source files.

In a header file you declare a variable as extern:

header.h:

extern char word[15];

Then the source files that actually defines the variable word and those that reference it must include header.h at the beginning:

source1.c:

#include "header.h"

To make the variable word visible you declare it as global, for example you can define it in the main.c file:

#include <stdio.h>
....
#include "header.h"

char word[15];

Then it will be visible to all other object linked.

For further explanations see for example this post .

I'm not sure that I fully understood what you are trying to do in the code (I hope is just a test code or an exercise) however, if you only need the variable word to be visible (and not words ) then I would define both in the file that contains the main() . Declare word as global and also as extern in the header file.

The main source file should look like that:

#include <stdio.h>
....
#include "header.h"

char word[15];

int main () {

...

    char words[10][15] = { "seanna", "yellow", "marshmallow", "potato", "beach", "icecream", "seven", "giraffe", "random", "xylophone" };

...

    for(a = 0; a < 15; a++)
    {
        word[a] = words[randomIndex][a];
    }

...

return 0;
}

EDIT

If you just need to pick up a random word from the array words then it is better to use an approach like in the following:

char *words[10] = { "seanna", "yellow", "marshmallow", "potato", "beach", "icecream", "seven", "giraffe", "random", "xylophone" };
char *word;

int main()
{
    int idx = -1;

    srand (time(NULL));
    idx = rand() % 10;
    printf("Row: %d\n", idx);

    //Stores the whole word in the word array
    word = one_word(idx);
    printf("word: %s\n", word);

    //Finds the word length
    int wordlength = strlen(word);
    printf("word length: %d\n", wordlength);

    return 0;
}

As words is defined and assigned as an array of string literals you don't have to specify the size, it will be computed automatically and the nul (terminating) character will be added. Then if you don't need to modify the word, you can use just a pointer to the current extracted word. Otherwise to copy the word I suggest you to use memcpy (not copy one char at time).

word = malloc(16 * sizeof(char)); // 15 + 1 for the terminating char 
wordlen = strlen(words[idx]) + 1; // copy also the terminating char 
memcpy(word, words[idx], wordlen * sizeof(char))

(Here you should remove the sizeof(char) as it will be always 1, I put it there just to show that memcpy requires a size_t field)

Above both word and words are declared as global and will be visible to all the function plus to other source file if, in those files that reference them, you declare them at the beginning as extern (or use an header file as described above).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM