谁能告诉我为什么当我尝试在菜单驱动的数组操作程序中调用 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?
我正在尝试编写一个菜单驱动程序来执行创建、显示、插入和删除数组元素等操作。创建和显示函数工作得很好,但每当我尝试调用插入或删除函数时,程序就会崩溃,消息显示为进程已返回。我在下面分享了我的代码。我意识到创建功能并不理想,但我只是被告知要这样做。如果相关的话,我正在使用 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");
}
感谢您的帮助!
在您的 insert
和 deletes
函数中,您没有取消引用作为参数给出的指针 (n
)。您需要将 *
运算符添加到此变量以获取或设置 n
计数器的实际值。
对于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");
}
}
deletes
函数所需的更改非常相似。
发现了问题,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;
函数现在看起来像这样:
void insert(int a[], int n, int pos, int ele)
void deletes(int* a, int n, int pos)
我正在尝试编写一个菜单驱动程序来执行创建、显示、插入和删除数组元素等操作。创建和显示函数工作得很好,但每当我尝试调用插入或删除函数时,程序就会崩溃,消息显示为进程已返回。我在下面分享了我的代码。我意识到创建功能并不理想,但我只是被告知要这样做。如果相关的话,我正在使用 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");
}
感谢您的帮助!
在您的 insert
和 deletes
函数中,您没有取消引用作为参数给出的指针 (n
)。您需要将 *
运算符添加到此变量以获取或设置 n
计数器的实际值。
对于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");
}
}
deletes
函数所需的更改非常相似。
发现了问题,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;
函数现在看起来像这样:
void insert(int a[], int n, int pos, int ele)
void deletes(int* a, int n, int pos)