0

i have an assignment for collage to build a dynamic dictionary, the assignment must be used with pointers and dynamic allocation ( i understand that this is trivial stuff that i am saying here). any how i have been trying to play with the code and this is what i have come up with. im sure i have mistakes in here, my problem is actualy how do implement the memory allocation right, and then how to store the words/definitions in that memory that i allocate.

 printf("Please enter the amount of words in the dictionary.\n");
scanf("%d",&numOfWords);
getchar();
wordsArr = (char***)malloc(3 * sizeof(char**));
for (int i = 0; i < numOfWords; i++)
{
    printf("Please enter the word and how many defenitions it has (1 or 2).\n");
    scanf("%s %d",&wBuffer[i],&defs);
    getchar();
    wordsArr[i]=(char**)malloc(strlen(wBuffer)*sizeof(char*)+1);
    strcpy(*wordsArr[i],wBuffer);
    for (int j = 0; j < defs; j++)
    {
        printf("Enter the %d is:\n",i);
        gets(dBuffer);
        wordsArr[i][j]=(char*)malloc(strlen(dBuffer)*sizeof(char)+1);
        strcpy(wordsArr[i][j],dBuffer);
    }
}
//Dont mind this just for the print test    
for (int i = 0; i < numOfWords; i++)
{
    for (int j = 0; j < defs; j++)
    {
        printf("%s",wordsArr[i][j]);
    }
}

}

SoNiC306
  • 5
  • 5
  • Why not use a hash table....or an array list....or some data structure – Marshall Tigerus May 02 '14 at 15:23
  • The teacher have restricted us not to use anything of that matter, only to use functions, pointers,strings,arrays and dynamic allocations,. – SoNiC306 May 02 '14 at 15:26
  • Fun stuff. You know data structures can all be built on arrays, poitners, strings and dynamic allocations, but that's the evil student in me trying to stick it to the teacher – Marshall Tigerus May 02 '14 at 15:38
  • hehe if i could use data structures i would get this "project" already, but because i cant use them, im stuck, and the code above is only thing i could think about so far... basicly what i need is X words that each word has 1 or 2 defenitions (arr[][][]) prolly. something like that. and i know that the code above is one big WTF because its not working. – SoNiC306 May 02 '14 at 15:40
  • 2d array, first dimension for word number, second for definition number. Most of what you have seems correct, but not very dynamic. Be sure to free malloced memory when you are done – Marshall Tigerus May 02 '14 at 15:41
  • problem is that in that code, thats just the allocation of the memory... i try to use [strcpy] to acualy put the word in the array and it wont let me. something because char*** != char ** – SoNiC306 May 02 '14 at 15:44
  • any suggestion on what i can do to make it more efficient and work? – SoNiC306 May 02 '14 at 15:47
  • I'm a little rusty on C with no compiler handy, try answer below – Marshall Tigerus May 02 '14 at 15:49
  • alright, thanks, i assume this is for the allocation of the memory only? – SoNiC306 May 02 '14 at 15:52
  • yes the rest should be okay/work, obviously with some modification. Its an array of pointers, so strcpy should work find on it – Marshall Tigerus May 02 '14 at 15:52
  • can you post your updated code? – Marshall Tigerus May 02 '14 at 16:46
  • updated, after the strcpy command it crashes. and i get in the wordsArr : error reading char string... etc. – SoNiC306 May 02 '14 at 16:49
  • wordArr[i] would point to an array of string arrays. I see the problem. You need to change the malloc(2 * sizeof(char*)) to malloc(3*sizeof(char*) and store the dictionary word in wordArr[i][0] with its definitions in wordArr[i][1] and wordArr[i][2] – Marshall Tigerus May 02 '14 at 17:21
  • easy said hehe VERY hard for me to do :). – SoNiC306 May 02 '14 at 18:00

1 Answers1

1

Maybe try this (no C compiler handy)

char ***array;
array = malloc(numOfWords * sizeof(char**));
for (i = 0; i < rows; i++)
   array[i] = malloc(3 * sizeof(char*)); // assumign 2 is your max definitions

This should make a memory allocation of numOfWords pointers to an array of character arrays, with 3 character arrays, the first being the dictionary word and the other two being definitions.

Marshall Tigerus
  • 3,675
  • 10
  • 37
  • 67