c - 如何删除指针数组的第一个值?

标签 c arrays pointers

我正在尝试编写一个函数(Remove_Item),该函数获取指针数组以及数组的长度,并删除第一个指针。 这是代码:

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

 void Print_Menu();
 void Clear_Queue(int ***list,int size);
 void Add_Item(int ***list,int size);
 void Remove_Item(int ***list,int size);
 void Print_Queue(int **list,int size);
 void Print_MaxMin(int **list,int size,int Max_or_Min);
 void Print_Index(int **list,int value,int size);


 int main()
 {
   int size_list = 0,input = 1,new_size,value;
    int **FIFO = (int**)malloc((size_list+1)*sizeof(int*));
   FIFO[0] = NULL;
    Print_Menu();

   while(input != 0)
    {
     scanf("%d", &input);

     switch(input)
        {
          case 0:
           Clear_Queue(&FIFO,size_list);
           return;
          case 1:
           size_list++;
           Add_Item(&FIFO,size_list);
                break;
          case 2:
           Remove_Item(&FIFO,size_list);
           --size_list;
                break;
          case 3:
            Print_Queue(FIFO,size_list);
            break;
          case 4:
            Print_MaxMin(FIFO,size_list,1);
            break;
           case 5:
            Print_MaxMin(FIFO,size_list,-1);
            break;
           case 6:
            printf("Please enter value to find index\n");
            scanf("%d",&value);
            Print_Index(FIFO,value,size_list);
            break;
           case 7:
            Clear_Queue(&FIFO,size_list);
            size_list = 0;
            break;
           case 8:
            Print_Menu();
            break;
           default:
            printf("Error: Unrecognized choice\n");
        }
        if (input != 8)
         printf("Please select your next choice (select 8 for complete menu)\n");
    }
    return;
 }

 void Print_Menu()
 {
    printf("Please select your choice:\n");
    printf("0.Exit\n");
    printf("1.Add item to the queue\n");
    printf("2.Remove item from the queue\n");
    printf("3.Print queue\n");
    printf("4.Print the maximum item in the queue\n");
    printf("5.Print the minimum item in the queue\n");
    printf("6.Print index of given item\n");
    printf("7.Clear queue\n");
    printf("8.Print the menu\n");
 }


 void Clear_Queue(int ***list,int size)
 {
    int i=0;
    for(i=0 ; i<size ; i++)
    {
       free((*list)[i]);
       (*list)[i] = NULL;
    }
   printf("is clear\n");

 }

 void Add_Item(int ***list,int size)
 {
    int i=0,value;

    *list = (int**)realloc(*list,(size)*sizeof(int*));

    if((*list) ==NULL)
    {
       printf("Error\n");
       Clear_Queue(list,size);
       return;
    }
    (*list)[size-1] = (int*)malloc(1*sizeof(int));

    if((*list)[size-1] == NULL)
    {
       printf("Error\n");
       Clear_Queue(list,size);
    }

    printf("Enter item value to add\n");
    scanf("%d",&value);
    (**list)[size-1] = value;
    printf("item %d added\n",(**list)[size-1]);

 }

 void Remove_Item(int ***list,int size)
 {
    int i=0,item,tmp;

    if((*list)[0] == NULL)
    {
       printf("Error queue is empty\n");
       return;
    }

    item = *((*list)[0]);

    for(i=0 ; i<size-1 ; i++)
    {
        *((*list)[i]) = *((*list)[i+1]);
    }
    *list = (int**)realloc(*list,size-1);
     printf("item %d was removed\n",item);
 }

 void Print_Queue(int **list,int size)
 {
    int i=0,item;
    if(list[0] == NULL)
    {
       printf("Error queue is empty\n");
       return;
    }

    for(i=0 ; i<size ; i++)
    {
       item = (*list)[i];
       printf("%d ",item);
    }

    printf("\n");
    return;
 }

 void Print_MaxMin(int **list,int size,int Max_or_Min)
 {
    int i,j,max,min;
    if((list)[0] == NULL)
    {
       printf("Error queue is empty\n");
       return;
    }
    max = (*list)[0];
    min = (*list)[0];
    for(i=1 ; i<size ; i++)
    {
       if(((*list)[i] > max)&&(Max_or_Min == 1))
       {
          max = (*list)[i];
       }
       if(((*list)[i] < min)&&(Max_or_Min == -1))
       {
          min = (*list)[i];
       }
    }
    if(Max_or_Min == 1)
    {
       printf("The maximum value is %d\n",max);
    }
    if(Max_or_Min == -1)
    {
       printf("The minimum value is %d\n",min);
    }
 }   

 void Print_Index(int **list,int value, int size)
 {
    int i,current,index_found=0;

    if(list[0] == NULL)
    {
       printf("Error queue is empty\n");
       return;
    }

    for(i=0 ; i<size ; i++)
    {
       current = (*list)[i];
       if(current == value)
       {
          printf("The index of %d is %d\n",value,i);
          index_found++;
       }
    }
    if(index_found == 0)
    {
       printf("Error value not in array\n");
    }
 }

问题在于,当输入为1 2 3时,输出为0 2。而不是 删除第一个元素,会将第一个元素变为零并删除最后一个元素。

最佳答案

我的用于删除数组中第一个指针的代码:

#include <stdio.h>
#include <stdlib.h>
void removep(int *, int);
int main() {
    int * arr = malloc(3*sizeof(int *));
    arr[0] = 1;
    arr[1] = 5;
    arr[2] = 10;
    printf("%d %d %d\n", arr[0], arr[1], arr[2]);
    removep(arr, 3);
    printf("%d %d", arr[0], arr[1]);
    return 0;
}

void removep(int arr[], int n) {
    int i;
    for(i = 0; i < n - 1; i++) {
        *(arr+i) = *(arr+i+1);
    }
    arr = realloc(arr, (n-1)*sizeof(int *));
}

输出:

1 5 10 5 10

希望有帮助。

关于c - 如何删除指针数组的第一个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41555472/

相关文章:

c - OpenGL ES 1 剪切对象

用于返回重复一定次数的字符串的 C 预处理器宏

php - 自然地按键排序多维数组

c++ - 涉及前/后增量和指针的 C 程序输出

C - 在函数之间传递数组得到不同的值

c++ - 避免 C++ 类中的内存泄漏

C - 给定两个数组,你如何找到其中一个数组的唯一元素的数量?

c - 代码中的缓冲区溢出识别

c++ - 遍历数组最有效的方法是什么? (c++)

c# - 在 C# 列表容器中查找和/或访问值