Question

I have gone under a problem while coding in c (i am not experienced programmer). I have a structure like this

struct afreq
{
    int freq;
    unsigned char sym;      
    short int left,right,next;      
};

in main function :

struct afreq data[50] ;  

int size = manipulation(data, count); //this function do some manipulation and returns size of int type.And i use this size in the function call below:

dictionary(data,var,size);

The problem creating part is here:

///////////////////////////// Definition of Dictionary function /////////////////////////////////////////


  dictionary(struct afreq data[] ,char *var,size_t dataSize)// function definition create problem in first argument when caled from the another recursive function call inside this function.
dictionary(struct afreq data[] ,char *var,size_t dataSize)
    { 
    int i;
    printf("\n data: %d\n", dataSize);
    for(i=2;i<dataSize;++i)
    {
    if(data[i].left=='\0')
    { int correspondance[data[30];
       char temp[30];
       strcpy(temp, var);
        strcat(temp, "0");
        printf("check1");   
     dictionary(data[correspondance[data[i].left]], temp,dataSize); //error here
    }  
    printf("\n");
        }
    }

What i want to do using this? I have to read the alphabets from a file given as sole argument. The file is "Input.txt" and contains inside 'abcdef' (i calculate their frequency in my program) and then i save them in an array in the format [symbol Frequency LeftChild RightChild] (example: [a 1 0 0] [b 2 0 0 ] [c 3 a b] etc.(it's like huffman code)). Up to here i have done everything properly. But when i try to print the dictionary like (as we have in huffman (path in o and 1)) (In the example we can see that "c" is parent and path of "a" is "o" and "b" is "1"). To implement this part i have written the code above, which creates error in first argument of function call. Here is the complete code (But please do not forget to include "Input.txt" file at sole argument which contains "abcdef" .

Here is the output:



 hp@ubuntu:~/Desktop/Internship_Xav/Task2$ gcc ttask.c -o ttask
improve.c: In function ‘dictionary’:
improve.c:85:2: error: incompatible type for argument 1 of ‘dictionary’
improve.c:74:1: note: expected ‘struct afreq *’ but argument is of type ‘struct afreq’
Was it helpful?

Solution

The error is, essentially, what your compiler says it is. You are passing, as first argument, data[i].left, which is of type short int, whereas a pointer to struct afreq is needed. The only reason why it compiles anyway is because gcc, by default, allows conversions between int and pointer (but there's not many reasons why you would want to do that).

If I understand correctly what you are trying to do - you want to call recursively your "dictionary" function on what seems to be a tree. The problem is that that structure is not really well defined. You are trying to "index" the nodes of the tree with their value (that's what I understand with the data[i].left), but there is no way to "make the link" between, say, 'a', and "the struct afreq that has 'a' as a symbol. You either need to make a correspondance table somewhere and make a call to it, or you need to transform left and right in your structure into pointers to the corresponding structure.

Good luck!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top