我正在编写一个C程序,为动态字符串数组创建交互界面.该程序是菜单驱动的,并遵循以下步骤:

  1. 询问用户数组的初始长度.
  2. 将相应的字符串存储到一个用该长度初始化的动态数组中.
  3. 启动菜单循环,提示用户 Select 以下五个选项之一: (a)将一个字符串添加到数组的末尾, (b)将一个字符串添加到数组的开头, (c)删除数组索引'x'处的元素(作为输入), (d)显示数组的长度, (e)按顺序显示数组的所有元素.
  4. 添加和删除字符串会导致调整数组的大小.
  5. 提供第六个选项来关闭菜单驱动的程序,从而终止循环.

代码:

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

typedef struct UserStrings
{
    char in[100];
} input;

int main(void)
{

    int n;
    printf("Enter size of Array, please: \n");
    scanf(" %d", &n);
    input *names = (input *)malloc(n * sizeof(input));

    for (int i = 0; i < n; ++i)
    {
        printf("Enter String Number %d : ", i + 1);
        scanf("%s", names[i].in);
    }

    printf("%d\n",n);
    char temp = ' ';

    while (temp != 'f')
    {
        printf("Enter -a- to add an element at the end of the array\n"
               "Enter -b- to add a string at the beginning of the array\n"
               "Enter -c- to delete an element at an index\n"
               "Enter -d- to display the length of the array\n"
               "Enter -e- to display all elements\n"
               "Enter -f- to terminate this program\n");

        scanf("%s", &temp);

        if (temp == 'a')
        {
            names = (input *)realloc(names, (n + 1)*sizeof(input));
            printf("Enter the string\n");
            scanf("%s", names[n].in);
            ++n;
            printf("%d\n",n);
        }
        else if (temp == 'b')
        {
            names = (input *)realloc(names, (n + 1)*sizeof(input));
            ++n;
            printf("Enter the string\n");

            for (int i = n - 1; i > 0; --i)
            {
                strcpy(names[i].in, names[i - 1].in);
            }
            scanf("%s", names[0].in);
            printf("%d\n",n);
        }

        else if (temp == 'c')
        {
            printf("Enter the index 0-%d: ", n - 1);
            int ind;
            scanf(" %d", &ind);

            if (ind < 0 || ind >= n)
            {
                printf("Invalid index. Please enter a valid index.\n");
                continue; // Skip the rest of the loop iteration
            }

            input *new = (input *)malloc((n - 1) * sizeof(input));

            int flag = 0;
            for (int i = 0; i < n; ++i)
            {
                if (i == ind)
                {
                    flag = 1;
                    continue; // Skip the element at the specified index
                }

                new[i - flag] = names[i];
            }
            free(names);
            names = new;
            new = NULL;
            --n;
            printf("%d\n",n);
        }
        else if (temp == 'd')
        {
            printf("Lenght of the array is %d\n", n);
        }
        else if (temp == 'e')
        {
            for (int i = 0; i < n; ++i)
            {
                printf("%d is : %s \n", i + 1, names[i].in);
            }
        }
        else if (temp == 'f')
        {
            printf("Program terminated!\n");
            return 1;
        }
        else
            printf("PLEASE ENTER IN SMALL CAPS AND CORRECT INPUT\n");
    }
}

我try 使用printf语句调试代码,但我丢失了n的值.当我初始化数组时,它似乎具有期望值.但当我try 任何操作后,n的值发生了意外的变化.以下是输出.

请帮帮我

Enter size of Array, please:
2
Enter String Number 1 : A
Enter String Number 2 : B
2 // This is the output of printf. So n is fine here.
Enter -a- to add an element at the end of the array
Enter -b- to add a string at the beginning of the array
Enter -c- to delete an element at an index
Enter -d- to display the length of the array
Enter -e- to display all elements
Enter -f- to terminate this program
e // No elements are printing.
Enter -a- to add an element at the end of the array
Enter -b- to add a string at the beginning of the array
Enter -c- to delete an element at an index
Enter -d- to display the length of the array
Enter -e- to display all elements
Enter -f- to terminate this program
d
Lenght of the array is 0 /* Length of array is 0? In the whole code, I cant spot why n will change using these operations. */

推荐答案

发生问题的一个可能原因是溢出.特别是,您定义了

char temp = ' ';

然后使用以下命令获取新的值

scanf("%s", &temp);

它读取的是string,而不是字符,因此即使是一个字符字符串也会将2个字节读入temp的位置.然而,它只有1个字节,所以堆栈上的下一个字节将被覆盖,这显然是您对n的声明,即,

int n;

然而,即使这不是特定的原因,它仍然会导致UB(未定义的行为).一个简单的解决方案是在scanf中使用%c而不是%s,即,

scanf("%c", &temp);

如果这一更改解决了问题,那么我确定的这个问题很可能是导致问题的原因.我不认为您的代码有任何其他问题,特别是那些可能导致您所看到的问题发生的问题.

C++相关问答推荐

从C函数调用asm函数时生成错误的BLX指令(STM32H753上的gcc)

为什么getchar()挂起了,尽管poll()返回了一个好的值?""

以c格式打印时间戳

字符数组,字符指针,在一种情况下工作,但在另一种情况下不工作?

当main函数调用被重构时,C函数给出错误的结果

C语言中字符数组声明中的标准

ZED for SDL上的C语言服务器

LibpCap禁用监视器模式(C、MacOS)

二进制计算器与gmp

如何使用指向 struct 数组的指针并访问数组中特定索引处的 struct

OpenSSL:如何将吊销列表与SSL_CTX_LOAD_VERIFY_LOCATIONS一起使用?

Zlib:解压缩大文件导致";无效代码长度设置";错误

为什么WcrTomb只支持ASCII?

Valgrind用net_pton()抱怨

在下面的C程序中,.Ap0是如何解释的?

OMP并行嵌套循环

Makefile无法将代码刷新到ATmega328p

atoi函数最大长-长误差的再创造

从系统派生线程调用CRT

GCC认为这是一个VLA是对的吗?