Porl123 Posted February 8, 2011 Share Posted February 8, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/227069-c-creating-a-2d-array-of-character-arrays/ Share on other sites More sharing options...
Zyx Posted February 8, 2011 Share Posted February 8, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/227069-c-creating-a-2d-array-of-character-arrays/#findComment-1171511 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.