0

I have an assignment to make a dictionary. It will contain an x amount of words and their definitions (input by user). Instructions say that the dictionary should be of type char*** (2D array of pointers=arrays=strings), but I've got absolutely no idea of how to dynamically allocate the size of the array. it should have 2 lines, 1 for words and another 1 for their definitions, and the number of columns is decided by how many words are in the dictionary. While looking for help online i came up with this:

char** AllocateArray(int line, int column)
{
    char** pArray=(char**)malloc(line*sizeof(char*));
    int i;
    for(i=0;i<2;i++) 
        pArray[i]=(char*)malloc(column*sizeof(char));

    return pArray;
}

What changes should i make in the code for it to work with my char*** ? Using Visual studio 2012

Edit:

I have a problem with this right now:

void inputString(char* p1)
{
    char buffer[80];
    printf("\nEnter a word:");
    scanf("%s",buffer);
    p1=(char*)malloc(strlen(buffer)+1);
    if(p1!=NULL)
    {
        strcpy(p1,buffer);
        free(buffer);
    }
}

it crashes right after i input a word. the char* that the function receives is dictionary[i][j]. –

Fiddling Bits
  • 8,712
  • 3
  • 28
  • 46
user3105173
  • 435
  • 4
  • 15

4 Answers4

2

Don't free() anything allocated on the stack (i.e. buffer). Also, your function inputString() will not tell its client what memory it had allocated, since p1 is local to it.

0

Here is an example.

char*** dictionary;
int i = 0;
int j = 0;
int lines = 10;
dictionary = (char***)malloc(sizeof(char**)*lines);

for(i=0;i<lines;i++)
{
    dictionary[i] = (char**)malloc(sizeof(char*)*4);
    for(j=0;j<4;j++)
        dictionary[i][j] = (char*)malloc(sizeof(char)*25);
}

You have to modify the malloc's parameters in order to adapt to your problem/ or modify them when you need more memory for your strings. Also it might be a good idea to try and free memory when you do not need it

Silent Control
  • 614
  • 10
  • 22
  • [You Shouldn't cast the results of malloc](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858#605858) – Leeor Dec 15 '13 at 19:47
  • I agree that in C (and not C++) it is not necessary, but writing the casts at least while he's a beginner in dynamic memory allocation is a good practice, because it helps him understand better pointers (which are his main problem). – Silent Control Dec 15 '13 at 19:51
0

Don't forget to malloc like this...

dictionary[i][j] = (char*)malloc(sizeof(char)*strlen(word_to_insert)+1);

...because each word end with a supplementary byte filled with 0 "null terminate string".

Fiddling Bits
  • 8,712
  • 3
  • 28
  • 46
SRedouane
  • 498
  • 5
  • 10
0

a sample

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
    char ***dictionary;
    const char *words[] = { "ASEAN", "United Nations", "OPEC" };
    size_t howManyWords = sizeof(words)/sizeof(*words);
    int i;

    dictionary = malloc(howManyWords*sizeof(char**));
    printf("Please enter the definition of this word\n");
    for(i = 0; i < howManyWords; ++i){
        char buff[80];
        char **keyValue;

        printf("%s : ", words[i]);
        fgets(buff, sizeof(buff), stdin);
        keyValue = malloc(2*sizeof(char*));
        keyValue[0] = (char*)words[i];
        keyValue[1] = malloc(strlen(buff)+1);
        strcpy(keyValue[1], buff);
        dictionary[i] = keyValue;
    }

    //print
    for(i=0;i<howManyWords;++i){
        printf("%s : %s", dictionary[i][0], dictionary[i][1]);
    }
    //release
    for(i=0;i<howManyWords;++i){
        free(dictionary[i][1]);
        free(dictionary[i]);
    }
    free(dictionary);
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70