c - 在处理 C 中数组的最后一行后,While 循环卡住了

标签 c arrays if-statement char

下面是我的代码,它卡在函数 int Ms_SEnd(char **p_note_for_sm) 中的 if((p_note_for_sm[msg_line_no]) != NULL) 处。调试 if 条件后,我发现在处理数组的最后一个字符后,没有打印 p_note_for_sm[msg_line_no] 的值,并且程序无法从 while 循环中中断,并且程序被卡住。我希望 p_note_for_sm[msg_line_no] 中的 NULL 值能够打破此循环。

请帮我找出解决方案。

#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <math.h>
#define SMAXLINE              200

int Ms_SEnd(char **p_note_for_sm)
{
    int msg_line_no = 0;

    while (msg_line_no <= 200)
    {
        if ((p_note_for_sm[msg_line_no]) != NULL)
        {
            if (strcmp(p_note_for_sm[msg_line_no], "\0") != 0)
            {
                printf("\nMsg In SM loop inside [%s]", p_note_for_sm[msg_line_no]);
            }
            else
            {
                break;
            }
            msg_line_no++;
        }
        else
        {
            break;
        }
    }
}

int main()
{
    char *chr_h_note_for_sm[SMAXLINE];
    char chr_h_note_1_new[8000] = "\0";
    char chr_h_note_new[8000] = "1 RECITE PREVIOUS MINUTES OF LAST AGM  2 DISCUSS THE BOD REPORT  3 DISCUSS THE AUDITORS REPORT  4 DISCUSS THE GENERAL BUDGET WITH THE PROFIT AND LOSS AND DISCUSS THE PERCENTAGE OF  CASH DIVIDENDS THAT WILL BE DISTRIBUTED  5 INDEMNIFY THE BOD  6 ELECT THE AUDITORS FOR THE YEAR 2019  7 DISCUSS OTHER ISSUES";
    int l_tail_pos = 0;
    int l_counter = 0;
    int msg_line_no = 0;
    int rownum = 1;
    int l_temp = 0;
    int l_length = 305;
    while (l_tail_pos < l_length)
    {
        chr_h_note_for_sm[l_counter] = (char *)calloc(36, sizeof(char));

        if ((l_length - l_tail_pos) < 35)
        {
            for (l_temp = 0; l_length > l_tail_pos; l_temp++)
            {
                chr_h_note_for_sm[l_counter][l_temp] = chr_h_note_new[l_tail_pos++];
            }
            chr_h_note_for_sm[l_counter][l_temp] = '\0';
        }
        else
        {
            strncpy(chr_h_note_for_sm[l_counter], chr_h_note_new + l_tail_pos, 35);
            chr_h_note_for_sm[l_counter][36] = '\0';
        }
        l_counter++;
        l_tail_pos = l_tail_pos + 35;
    }

    Ms_SEnd(&chr_h_note_for_sm);
    return 0;
}

最佳答案

您永远不会将 chr_h_note_for_sm 中的最后一个指针(因此 p_note_for_sm)设置为 NULL。此 if 语句不会看到字符串中的 NULL 值,因为这需要您取消引用 p_note_for_sm[msg_line_no]。解决方案是在调用 Ms_SEnd 之前在 chr_h_note_for_sm 末尾添加一个 NULL 指针,如下所示:

chr_h_note_for_sm[l_counter] = NULL;
/* l_counter is already at the correct index 
   due to the l_counter++ at the end of the while loop.*/
Ms_SEnd(&chr_h_note_for_sm);

现在 while 循环将中断,因为 p_note_for_sm 中有一个 NULL 指针。

关于c - 在处理 C 中数组的最后一行后,While 循环卡住了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54810303/

相关文章:

python - 在列表中查找-1s、1s 和 0s 的多数票 - python

ios - iPhone/iPad 宏或 c 函数?

php - 引用 - 这个错误在 PHP 中意味着什么?

mysql - SQL 语法错误 #1064 游标 (mysql)?

Javascript 从对象中获取相应的键/值

arrays - 使用常量内存在 O(n) 中对 BST 进行排序

c# 我如何比较两个具有匹配字母但一个有空格的字符串

c - Arduino C - 审查变量数据

c - 如何使用 GLUT 进入指定分辨率的全屏模式?

c++ - GCC:使用 -Werror 但将特定错误降级为警告(在命令行中)