- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个函数,它遍历我的字符串数组以找出该字符串在数组中出现了多少次。如果找到,该字符串将被设置为 NULL
并且一个计数器会跟踪该字符串被找到的次数。然后我在循环中调用另一个函数来为我的频率数组分配内存,以便我可以存储 count
。它似乎工作正常,但是当我在 main 中创建任何其他变量时,我的程序崩溃了。这是我的两个功能:
int search(char **table, int **frequency, int wordSize)
{
// Local Declaration
int i, j, k;
int count = 1;
int strCount = 0;
char target[25];
// Statement
for(i = 0, k = 0; i < wordSize; i++)
{
if(table[i] != NULL)
{
strcpy(target, table[i]);
for(j = i + 1; j < wordSize; j++)
{
if(table[j] != NULL &&
strcmp(target, table[j]) == 0 &&
target != table[i])
{
count++;
free(table[j]);
table[j] = NULL;
}
}
strCount += makeFreq(frequency, k, count);
k++;
}
count = 1;
}
return strCount;
}// search
int makeFreq(int **frequency, int k, int count)
{
// Local Declaration
int strCount = 0;
// Statement
frequency[k]=(int*)malloc(sizeof(int));
frequency[k][0] = count;
strCount += 1;
return strCount;
}// makeFreq
有人可以向我解释为什么我的程序崩溃了吗?
这里我为我的表分配了 1000 个指针。
char** getPoint(void)
{
// Local Declaration
char **table;
// Statement
table = (char**)calloc(MAX_SIZE + 1, sizeof(char));
if(table == NULL)
{
MEM_ERROR, exit(100);
}
return table;
}// getPoint
比起我阅读,我为文件中的字符串分配内存并将其存储到字符串数组中。
int scanFile(char **table, FILE *fpFile)
{
// Local Declaration
int count = 0;
char temp[500];
char **ptr = table;
// Statement
// scan file, allocate, and copy string to array.
while(fscanf(fpFile, "%s", temp) != EOF)
{
*(ptr + count) =(char*)calloc(strlen(temp)+1, sizeof(char));
strcpy(*(ptr + count), temp);
count++;
}
return count;
}// scanFile
下面是我如何为我的频率数组分配指针数组。
void aloFreqAry(int **frequency, int wordSize)
{
// Local Declaration
// Statement
frequency =(int**)calloc(wordSize + 1, sizeof(int));
if(frequency == NULL)
{
MEM_ERROR, exit(103);
}
return;
}// aloFreqAry
最佳答案
除了分配的大小问题(在table
的分配中应该是sizeof(char*)
,和sizeof(int*)
中分配频率
),
void aloFreqAry(int **frequency, int wordSize)
{
// Local Declaration
// Statement
frequency =(int**)calloc(wordSize + 1, sizeof(int));
if(frequency == NULL)
{
MEM_ERROR, exit(103);
}
return;
}// aloFreqAry
不为调用方的frequency
分配任何内容。它只是将内存分配给该指针的本地副本,并在函数返回时丢失该指针的句柄。
函数应该返回一个,而不是将 int**
作为参数,
frequency = calloc(wordSize + 1, sizeof(int*)); // size of a _pointer_ to int
if(frequency == NULL)
{
MEM_ERROR, exit(103);
}
return frequency;
你在调用者中分配的。
关于c - 使用C的频率阵列动态分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15122624/
我有一个关于将字符串分配给数组编号的问题。 我已经声明了字符串数组,例如。 String[] answer = {"yes", "no", "maybe"}; 如何在不使用这种方法的情况下将每个字符串
我正在为云数据库使用 Firebase 编写一个 Android 应用程序。它基本上是一个多项选择调查问题应用程序。导入到我的 Firebase { "multiple_choice" : {
我想将输入文件中的以下行存储到 3D 数组中(不包括第一行。)第一行表示后续行的数量。 3 4 9368 86 843 23224 4 7323 2 2665 2665 8447 47 843 527
这是我关于容器的小大问题,尤其是数组。 我正在编写一个物理代码,主要操纵一大组(> 1 000 000)“粒子”(每个粒子有 6 个 double 坐标)。我正在寻找最佳方式(在性能方面)来实现一个类
我有一个超链接,我需要在 Angular 4 中创建一个路由器链接。我有很多部分指向 url,其中一部分是一个数组。我不确定如何让数组将自己拆分成 routerlink 数组的部分。 以这个人为的例子
大家好,我有一个轮子选择器在工作,但目前它正在为所有轮子提取 0-9 的数字。我希望能够设置值而不是 0-9 我希望它是从数组或字符串中提取的单词,所以我可以输入它们 myslef 因为我不确定目前从
我正在尝试使用 Spotify API 并进入数组。 const App = () => { const [isLoading, setIsLoading] = useState(true);
我尝试创建 Tic Tac Toe,我能够填满我的棋盘,并且能够检查行和列以确定谁获胜。然而,我需要一些帮助来检查对角线,看看谁赢了。这是我到目前为止所拥有的。我是初学者,所以请不要让代码太难。 检查
--in the package type t_array is array (natural range <>) of std_logic_vector (7 downto 0); type p_a
我在访问字符串数组时遇到困难。它被声明为私有(private)数组并填充在类的构造函数中。我定义了一个 Get 函数。问题是当我在编译时调用此函数时出现错误,提示我无法访问在类中声明的私有(priva
无法弄清楚推送到 Moose 数组的语法(我确信这很明显,而且我很愚蠢)。这是 this question 的延续.在我看来,对于我的具体情况,我需要的不仅仅是一个简单的值。尝试使用 Moose 式的
我有一个 3d 数组,我正在尝试从中获取刺伤列表。换句话说,给定数组: t = np.array([[[1,2],[3,4]],[[5,6],[7,8]],[[9,10],[11,12]]]) arr
我正在寻找绘制一个 3 维数组。有没有一种方法可以直接输入数组,绘制体素并在 3d 数组中的位置产生的坐标处绘制实际值(颜色)?到目前为止我发现的所有方法(例如 ax.voxels、mlab.poin
我正在尝试使用 Knockout 创建一个简单的电子表格。我试图让每个单元格都可观察,以便在发生变化时,我可以评估值并进行相应的计算。因此,如果他们在单元格中输入 6+7,我可以评估并将该值更改为总数
我有当前时间和这组时间。我想计算出下一次与当前时间最接近的时间。 let date = NSDate() let calendar = NSCalendar.currentCalendar() let
我想在我的小程序中创建一个二维图像数组。我需要一个 4x4 网格,其中有 4 个图像,每个图像 4 个随机分布在阵列中。这里有一些答案,但我不明白如何使用它们。 最佳答案 您可以声明 Image[][
基本上,此代码列出了“可用”挑战,其中 complete = 0 并在每个列表中都有一个接受submit 按钮。到目前为止,我一次只能列出一项,因为列出的多个按钮无法识别匹配 ID $echo 任何人
我正在尝试创建一个带有动态变量的过滤数组。我创建一个包含过滤器键的数组,然后创建一个过滤后的数组,该数组只应返回与第一个数组中的键匹配的项目。 带有过滤器键的数组:$scope.participant
我是一个相对年轻的开发人员,我对一些事情感到困惑。 这是我的代码: function pairElement(str) { var arr = []; var pairs = [
我正在 Angular 中创建一个函数,我想抓取所有博客文章,其类别与单击的按钮相匹配,我的 Firebase 中有 3 个不同的字段,标题为类别 1、类别 2 和类别 3。例如,当用户单击新闻通讯时
我是一名优秀的程序员,十分优秀!