【发布时间】:2014-01-16 08:28:49
【问题描述】:
我在用 c 编码时遇到了问题(我不是经验丰富的程序员)。 我有这样的结构
struct afreq
{
int freq;
unsigned char sym;
short int left,right,next;
};
在主函数中:
struct afreq data[50] ;
int 大小 = 操作(数据,计数); //这个函数做一些操作和 返回 int 类型的大小。我在下面的函数调用中使用这个大小:
dictionary(data,var,size);
问题的产生部分在这里:
///////////////////////////// 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");
}
}
我想用这个做什么? 我必须从作为唯一参数给出的文件中读取字母。该文件是“Input.txt”并包含在“abcdef”中(我在我的程序中计算它们的频率),然后我将它们保存在一个数组中,格式为 [symbol Frequency LeftChild RightChild](例如:[a 1 0 0] [ b 2 0 0 ] [c 3 a b] 等(就像霍夫曼代码)。 到这里为止,我已经做好了一切。但是当我尝试打印字典时(就像我们在霍夫曼(o 和 1 中的路径)中所做的那样)(在示例中我们可以看到“c”是父级,“a”的路径是“o”和“b”是“1”)。为了实现这部分,我编写了上面的代码,这会在函数调用的第一个参数中产生错误。 这是完整的代码(但请不要忘记在包含“abcdef”的唯一参数中包含“Input.txt”文件。
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’
【问题讨论】:
-
data[i].left类型为short int -
那里有很多问题。
-
@BLUEPIXY 你对我的问题有什么解决方案吗(我不得不不使用指针,否则我会这样做。)你能帮我吗?谢谢。 (下面试图分析的人已经完全理解我要做什么。但是如何通过创建表格来实现它?(不是指针)请提供任何 sn-p 或指导?)
-
@Dipto 感谢您的帮助。实际上,“Blaise”完全理解了我的问题。您可以阅读以准确了解我想要实现的目标。但我不知道如何通过创建表来实现(因为不必使用指针)
-
我认为您应该仔细考虑应该将什么传递给函数。
标签: c function structure function-call