[英]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.在您的insert
和deletes
函数中,您不会取消引用作为参数给出的指针 ( 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.