简体   繁体   English

谁能告诉我为什么当我尝试在菜单驱动的数组操作程序中调用 Insert 或 Delete 函数时我的程序会崩溃?

[英]Can anyone tell me why my program crashes when I try to call the Insert or Delete function in a menu driven array manipulation program?

I'm trying to write a menu driven program to perform actions like create, display, insert, and delete elements from an array.我正在尝试编写一个菜单驱动程序来执行诸如创建、显示、插入和删除数组中的元素之类的操作。 The create and Display functions work just fine but whenever I try to call the insert or delete function, the program crashes and the message reads Process returned. create 和 Display 函数工作得很好,但是每当我尝试调用插入或删除函数时,程序都会崩溃并且消息读取进程返回。 I've shared my code below.我在下面分享了我的代码。 I realize that the create function is not ideal, but I've just been told to do it this way.我意识到 create 功能并不理想,但我刚刚被告知要这样做。 i'm using Code::Blocks 20.03 if that's relevant.如果相关,我正在使用 Code::Blocks 20.03。

#include <stdio.h>
#define MAX 50
void create(int[],int);
void display(int[],int);
void insert(int[],int*,int,int);
void deletes(int[],int*,int);

int main()
    {
        int ch,n,a[MAX],ele,pos;
        while(1)
        {
            printf("\nChoose an operation\n");
            printf("1. Create\n2. Display\n3. Insert\n4. Delete\n5. Exit\n\n");
            scanf("%d",&ch);

            switch(ch)
            {
                case 1: printf("Enter the no. of elements\n");
                scanf("%d",&n);
                create(a, n);
                break;

                case 2: display(a, n);
                break;

                case 3: printf("Enter the position at which you want to insert the element\n");
                scanf("%d",&pos);
                printf("Enter the element to be inserted\n");
                scanf("%d",&ele);
                insert(a, &n, pos, ele);
                break;

                case 4: printf("Enter the position of the element to be deleted\n");
                scanf("%d",&pos);
                deletes(a,&n,pos);
                break;

                case 5: exit(0);

                default: printf("Invalid Input");
                break;
            }
        }
    }

    void create(int a[], int n)
    {
        int temp;
        printf("Please enter the elements\n");
        for(int i=0; i<n ; i++)
        {
            scanf("%d",&temp);
            a[i] = temp;
        }
    }

    void display(int a[], int n)
    {
        printf("The array is \n");
        for(int i=0;i<n;i++)
           {
               printf("%d\t",a[i]);
           }
            printf("\n");
    }

    void insert(int a[], int*n, int pos, int ele)
    {
        if(n==MAX)
        {
            printf("Array Overflow. Cannot Insert element\n");
        }
        else if(pos>=0 && pos<=n)
        {
            for(int i=n-1; i>=pos; i--)
            {
                a[i+1] = a[i];
            }
            a[pos] = ele;
            n++;
            printf("Element inserted successfully\n");
        }
        else
        {
            printf("Enter a valid position\n");
        }
    }

    void deletes(int *a,int*n, int pos)
    {
        if(pos<=n)
        {
            for(int i = pos-1; i<n;i++)
            {
                a[i] = a[i+1];
            }
            n--;
            printf("The element has been deleted\n");
        }
        else
            printf("Invalid position");
    }

Thank you for your Help!感谢您的帮助!

In your insert and deletes functions, you are not dereferencing the pointer ( n ) given as an argument.在您的insertdeletes函数中,您不会取消引用作为参数给出的指针 ( n )。 You need to add the * operator to this variable in order to get or set the actual value of the n counter.您需要将*运算符添加到此变量中以获取或设置n计数器的实际值。

For the insert function, the corrected code is:对于insert函数,更正后的代码为:

void insert(int a[], int*n, int pos, int ele) // NOTE: "n" is a POINTER!
{
    if(*n==MAX) // Dereference n here
    {
        printf("Array Overflow. Cannot Insert element\n");
    }
    else if(pos>=0 && pos<=*n) // ... and here
    {
        for(int i=*n-1; i>=pos; i--) // ... and here
        {
            a[i+1] = a[i];
        }
        a[pos] = ele;
        (*n)++; // Note the brackets here - otherwise we increment the pointer!
        printf("Element inserted successfully\n");
    }
    else
    {
        printf("Enter a valid position\n");
    }
}

The changes required to the deletes function are very similar. deletes功能所需的更改非常相似。

found the problem, n is a simpel int not an array there is no need in sending it as an adress so what i did in switch case:发现问题, n是一个 simpel int 不是数组,不需要将它作为地址发送,所以我在 switch case 中做了什么:

case 3: printf("Enter the position at which you want to insert the element\n");
            scanf("%d", &pos);
            printf("Enter the element to be inserted\n");
            scanf("%d", &ele);
            insert(a, n, pos, ele);
            break;

        case 4: printf("Enter the position of the element to be deleted\n");
            scanf("%d", &pos);
            deletes(a, n, pos);
            break;

and the functions looks like that now:现在的功能看起来像这样:

void insert(int a[], int n, int pos, int ele)

void deletes(int* a, int n, int pos)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在制作一个菜单驱动程序,我必须在其中创建、删除和插入一个数组 - I am making a Menu driven program in which i have to create, delete and insert an array 为什么在尝试删除[]数组时程序会抛出SIGABRT? - Why does my program throws SIGABRT when I try to delete[] array? 谁能告诉我为什么我的数组表现不一致? - Can anyone tell me why my array is behaving inconsistently? 在数组上使用自由函数(C)时我的程序崩溃 - My program crashes when using free function (C) on array 当我尝试运行程序时,GUI无法加载,我无法弄清楚为什么 - When i try to run my program, the GUI won't load and I can't figure out why 我的程序无法编译,谁能帮我在哪里修复 - My program doesn't compilem, can anyone help me where should I fix Swift iOS。 当我尝试更改按钮数组时,程序崩溃 - Swift iOS. Program crashes when i try to mutate an array of buttons 当我尝试对指针数组中的数组进行free()时,程序在特定值上崩溃 - Program crashes on a specific value when i try to free() the arrays in a pointer array 当我使用int或unsigned long long程序崩溃时,什么数据类型可用于数组索引0到4000000 - What datatype can be used for array index 0 to 4000000 when i used int or unsigned long long my program crashes 我正在尝试从 a.csv 读取信息并将其放入 C# 中的数组中。 谁能告诉我为什么代码不起作用? - I am trying to read information from a .csv and put it into an array in C#. Can anyone tell me why the code doesn't work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM