#include<stdio.h>
#include<stdlib.h>
void DeleFromA(int *p, int n, int post)
{
if (post > n || post < 0)
{
printf("错误\n");
}
else
{
for (int i = post + 1; i < n; i++)
{
p[i - 1] = p[i]; //删除就是从post开始,把后面一项移到前面那一项
}
}
}
int main(int argc, char const *argv[])
{
int arr[] = {1, 2, 3, 4, 5};
int n = 5; //元素个数
int post = 4; //删除位置下标
DeleFromA(arr, n, post);
for (int i = 0; i < n - 1; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
C语言:封装函数,实现删除数组指定位置的数据
于 2024-08-17 20:13:39 首次发布