1

In a program, I have to scan from the input

scanf("%s",currWord);

a non-defined number of words, which come in a non-defined number of lines.

I want to put the words in a 2 dimensional array of strings. Length of the strings is fixed [MAX_WORD_LEN+1]

My idea is:

int row=10 //10 lines for starting
int col=5 //5 words in each line for starting
int i;

typedef char word[MAX_WORD_LEN+1]; //new type of 11char lenght string
word** matrix; //2 dimensional array (pointers) with no memory

matrix = malloc(row*sizeof(word*)); //allocate row number of word* (word pointer) size

for(i=0;i<row;i++)
{
  matrix[i] = malloc(col*sizeof(word)); //allocate col number of words to each row
}   

So, I have no idea if that is right.

I'll be happy for some help and tips..

EDIT:

When receiving the words from input I have to increase memory ( number of rows and words in each row) if needed, How do I do that? (realloc ?)

I need to do the following:

Image

6
  • 3
    Haven't we been here before? Commented Mar 21, 2013 at 7:54
  • Though this belongs on codereview, it looks correct if your intention is a grid of [MAX_WORD_LEN+1] sized char buffers. You may want to check malloc return values and such. I have to ask, is the ultimate goal to be a 2D layout of words from input, arranged in a dynamic list of words per line? Commented Mar 21, 2013 at 7:59
  • 1
    Your variable names are resulting in all this confusion IMHO. Consider this: typedef char** matrix and then declare your 2D array like: matrix wordList and proceed. Commented Mar 21, 2013 at 8:04
  • It's an exercise from a university course, that I am doing it to get prepared for my studies next year. The goal is to recieve an indefinite number of rows wich contains an undefinite number of words from the user, each row start with an index 0/1 , Then I do some manipualtion to the strings(words) according to the index and some other lists, after that I put those lines of words in the 2D array, becasue I have to print the lines after the manipualtion to all of them. Was I clear ? Commented Mar 21, 2013 at 8:26
  • 1
    @EL11 I somewhat figured. If that is the case, it all depends on how efficient you want to get, but your overall declarations look correct for the intended task of a grid. I suggest you look at the documentation for realloc(), but ultimately you'll need more information being tracked here than you currently have. Such as the number of words in each row, on a row-by-row basis. Draw on paper for awhile before you code it. It will help, believe me. Commented Mar 21, 2013 at 9:14

1 Answer 1

1

Without going into details, the easiest way is to use a Matrix as a Linked-List of Linked-Lists ..

struct matrix_field
{
    char data [11];
    matrix_field * next_field;
};

struct matrix_row
{
    matrix_field * first_field;
    matrix_row * next_row;
};

struct matrix
{
    matrix_node * first_row;
};

Your data will look like this in memory ..

[+]
 |
 v
[x]-->[a]-->[b]-->[c]-->
 |
 v
[y]-->[d]-->[e]-->[f]-->
 |
 v
[z]-->[g]-->[h]-->[i]-->
 |
 v

---------------

[+] matrix

[x] .. [z] matrix_row

[a] .. [i] matrix_field
Sign up to request clarification or add additional context in comments.

Comments

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.