这是我第一次制作结构数组。基本上我试图做一种直方图。这是分析我提供的字符串并打印出哪个字母出现次数最多。另外我想说我的程序确实有效!我得到了所有正确的结果。但是当程序完成计算并显示它们时,它会崩溃。我留下了一个错误说
IntelliSense: argument of type "Occurrences **" is incompatible with parameter of type "Occurrences *"
这段代码:
maximum_occurrences(str, ch, num, arr1);
位于我的main.c
。我将尝试提供下面所需的所有代码。关于如何在最后修复崩溃的任何想法都将非常感激,但程序逻辑再次完美! :)谢谢我也是一个新手编码器(C学期一个学期)。
主:
int main ()
{
// Introduced the original string which as a pointer.
// Introduced the original character pointer 'ch'. (Set to an arbitrary Character.)
// Introduced the original int pointer as 'num'. (Set to an arbitrary integer.)
char *str = {"hey my name is dillon johnson im in comp sci"}, *ch = '\0';
int *num = 0;
struct Occurrences *arr1[25];
// Calling the max occurrences function.
maximum_occurrences(str, ch, num, arr1);
return 0;
}
部首:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
typedef struct occurrences
{
int num_occurrences;
double frequency;
}Occurrences;
void maximum_occurrences (char *str, char *chPtr, int *num, Occurrences *arr1);
由source.c:
void maximum_occurrences(char *str, char *chPtr, int *num, Occurrences *arr1)
{
char stringp = *str;
int i = 0, j = 0, strLength = 0, temp, max = 0, k = 0;
double frequency = 0;
char maxch = ' ';
// Setting strLength to the length of the alpha string.
strLength = strlen(str);
for (k = 0; k < 25; k++)
{
arr1[k].num_occurrences = 0;
}
// Displaying to the user what sentance will be used for the program.
printf ("%s%s", "The given sentence is: ", str);
printf ("\n");
// The algorithm to determine which characters occur multiple times.
// I am using a for loop to first parse through the array and then when
// each letter occurs I subtract 97 from the ascii value. The resulting
// Integer will be the corresponding place in the array of struct Occurrences
// where that particular variable is held.
for (i = 0; i < strLength; i++)
{
temp = (str[i] - 97); // temp is now set to an integer value that is the correspoinding position in the array of structs.
(int)arr1[temp].num_occurrences = (int)arr1[temp].num_occurrences + 1; // Increases the number of occurrences in the specific struct by one to represnt
// a single occurrence of that character.
}
for (j = 0; j < 26; j++) // This finds the maximum amount of occurrences and then continues to mach that value
{ // to a character by adding 97 to the index of the array to acheive the appropriate ascii value
if (max < arr1[j].num_occurrences)
{
max = arr1[j].num_occurrences;
maxch = j + 97;
}
}
frequency = ((double)max / (double)strLength); // Calculating the frequency of the most common character
printf ("%s%c%s%d%s%.3lf%s", "The letter '", maxch,"' occurred the most, occurring ", max, " times, with a frequency of: ", frequency,".\n");
return;
}
答案 0 :(得分:2)
问题正是错误消息告诉你的。学会解释编译错误,生活对你来说会更容易......
arr1
是一个包含20个指向Occurrences结构的指针的数组。
maximum_occurrences
需要一个Occurrences*
,而不是它们的数组。
因此,您无法将arr1
传递给maximum_occurrences()
。
根据评论讨论:
在main()中你有struct Occurrences* arr1[25];
- 一个包含25个指向事件的数组(Occurrences *)。你永远不会分配任何出现次数,所以即使它确实编译了,它也不会起作用。
你想要25次出现(不是指针) - struct Occurrences arr1[25];
- 现在你应该可以按原样使用max_occurrences
了。请注意,让max_occurrences
假设长度为25是不好的做法 - 您应该将长度传递给它。
最后但并非最不重要的是,如果你想要每个字母出现1次,你需要一个26的数组(它给你的索引0-25)
答案 1 :(得分:0)
您需要将功能更改为
void maximum_occurrences(char *str, char *chPtr, int *num, Occurrences **arr1, int arrsize){
//..
for (k = 0; k < arrsize; k++)
{
arr1[k]->num_occurrences = 0;
}
//..
for (j = 0; j < arrsize; j++) // This finds the maximum amount of occurrences and then continues to mach that value
{ // to a character by adding 97 to the index of the array to acheive the appropriate ascii value
if (max < arr1[j]->num_occurrences)
{
max = arr1[j]->num_occurrences;
maxch = j + 97;
}
//..
}
然后用
调用它maximum_occurrences(str, ch, num, arr1, 25);
另外,在使用arr1
为每个数组元素调用maximum_occurrences()
之前,不要忘记为malloc(sizeof(struct Occurrences))
分配内存。