Jump to content

Recommended Posts

I'm trying to make a hang man game, which is fairly simple enough to make but I've run into a problem. The has the ability to select from the categories film and music at the moment. I'm trying to store the phrases in an array like this:

 

char phrases[0][0][10] = "Phrase1";

 

Although I know this syntax is wrong. The first [0] is the categories; 0 being films and 1 being music. The second is which phrase it's on and the third assigns 10 characters for the phrase. The error I receive from this 'the array must have at least one element', which I can understand. Although I'm not sure of how this statement is put. If anyone can help I'd appreciate it. Thanks

Is this array statically or dynamically allocated? Multidimensional, contignuous arrays are in fact one-dimensional arrays with a more complex index calculation and this is how I always implement them:

 

int *array = (int*) malloc(sizeof(int) * size_x * size_y);
array[x + y * size_y] = 13;

 

You can write a macro to wrap it and use more easily. String types are also pretty simple:

 

// allocate array memory
char **array = (char**) malloc(sizeof(char*) * size_x * size_y);
// allocate string memory
array[x + y * size_y] = (char*) malloc(sizeof(char) * 30);

// write something to the string
array[x + y * size_y][2] = 'z';

 

It will allocate an array of pointers to string arrays.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.