#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    21
    Rep Power
    0

    Unhappy **Two pointers array


    Hi guys, I need your help...
    I want to build a dictionary that the dictionary itself will be stored in a matrix with two rows of pointers to char.

    The idea is that in the first row (index zero) it will *save the word itself and the second row (index 1) in the same column will be stored the meaning of the word. At the start, the system will get the data that contains the user dictionary will build the dictionary according to the information and then enter the search phase.

    The search phase can search for words in the dictionary and get their meaning.

    I have not yet reached to the search part, cause I had difficulty defining the pointer array and frankly to understand how to work with that kind of dynamics...

    Can you tell me what I do wrong?

    Code:
    #define _CRT_SECURE_NO_WARNINGS 
    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h> 
    
    #define z 0
    #define r 2
    #define one 1
    #define cW 81
    #define cT 201
    
    int howMuchWords() {
    	int words;
    	printf("How much words you would like to Enter? (Integer Please)\n");
    	scanf("%d", &words);
    
    	return words;
    }
    
    void allocDic(char **arr[r], int size, int buff) { //maloc cols.
    	int i;
    	for (i=z; i<size; i++) {
    		if (arr[z] == NULL) {
    			printf("Couldn't Alloc\n");
    			break;
    		} else {
    			arr[i]= (char**)malloc((buff));
    		}
    	}
    }
    
    void feedDic(char **arr[r], char aWord[], char aTranslation[], int buff) {
    	int i;
    	for(i=one; i<=buff; i++) {
    
    		gets(aWord); 
    		gets(aTranslation); 
    
     		arr[z][i]= (char*)malloc(((strlen(aWord))+one)); //allocate space for the word. 
    
    		if (arr[z][z] == NULL) { //check if malloc fuction works
    			printf("Couldn't allocate memory\n");
    			exit(one);
    		}
    		strcpy(arr[z][i],aWord); //copies the word to the specifies place.
    
    		arr[z+one][i]= (char*)malloc(((strlen(aTranslation))+one)); //allocate space to the specifies place.
    		
    		if (arr[one][z] == NULL) { //check if malloc fuction works
    			printf("Couldn't allocate memory\n");
    			exit(one);
    		}
    		strcpy(arr[z+one][i],aTranslation); // copies the translate to the correct place.
    	}
    }
    
    void printDic(char **arr[r], int buff) {
    	int i;
    	for(i=z; i<=buff; i++) {
    		printf("%s",arr[i][z]);
    	}
    }
    
    int main() {
    	char **dic[r];
    	char word[cW], translation[cT];
    	int wordsNum = howMuchWords();
    
    	allocDic(dic,r,wordsNum);
    	feedDic(dic,word,translation,wordsNum);
    
    	printDic(dic,wordsNum);
    
    }
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481
    Assuming your program receives a valid integer howMuchWords will work. The program next goes to function allocDic.

    There quite soon it reads
    arr[z]

    The program must write to memory before reading from memory.


    After you restructure your program we'll happily move to the next problem, if you're still troubled.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    21
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Assuming your program receives a valid integer howMuchWords will work. The program next goes to function allocDic.

    There quite soon it reads
    arr[z]

    The program must write to memory before reading from memory.


    After you restructure your program we'll happily move to the next problem, if you're still troubled.
    Thanks for the quick response...
    You meant that check?
    Code:
    if (arr[z][z] == NULL)
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481
    Nope.

    The program calls allocDic which looks like this:
    Code:
    void allocDic(char **arr[r], int size, int buff) { //maloc cols.
    	int i;
    	for (i=z; i<size; i++) {
    		if (arr[z] == NULL) {
    All the names have initial values in ‘for (i=z; i<size; i++)’. Then the program checks ‘arr[z]’. arr[z] does not have a determined value. Algorithmic problems aside, this is the first grand error your program encounters.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    21
    Rep Power
    0

    Ok


    Orright, I figured this out . Thanks.. (:
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481
    If I had to make a dictionary in a c like language first of all I'd use a map type from the stl with c++.
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    21
    Rep Power
    0
    Originally Posted by b49P23TIvg
    If I had to make a dictionary in a c like language first of all I'd use a map type from the stl with c++.
    Well, I can't..
    Must do it with c. (;
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    21
    Rep Power
    0

    Smile


    Originally Posted by b49P23TIvg
    If I had to make a dictionary in a c like language first of all I'd use a map type from the stl with c++.
    Well, I can't..
    Must do it with c. (;
  16. #9
  17. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481
    Please post the program you've developed for the worthy advice of professional programmers at this forum.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo