0

Im trying to write a function(Remove_Item) that gets a pointer array along with the length of the array, and removes the first pointer. Here is the code:

 #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");
    }
 }

The problem is that when the input is 1 2 3, the output is 0 2. Instead of removing the first element, it turns the first one into zero and removes the last one.

5
  • 2
    Give us enough code to reproduce the problem. (Also, your realloc call can't possibly be right. The second parameter should be something like sizeof(int*)*(size-1).) Commented Jan 9, 2017 at 19:30
  • Perhaps you do not have enough stars. A three star programmer can write almost unreadable code, but get this. Commented Jan 9, 2017 at 19:46
  • Why are you storing pointers to integers, rather than the integers themselves, in the queue? That seems like an unnecessary complication. (And you're leaking memory.) Commented Jan 9, 2017 at 19:55
  • You may wish to rethink/simplify your design, or use typedef to improve the readability of your code. Commented Jan 9, 2017 at 19:57
  • Also please don't cast the return of malloc Commented Jan 9, 2017 at 20:00

2 Answers 2

1

My code for removal of first pointer in an array :

#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 *));
}

OUTPUT:

1 5 10 5 10

Hope that helps.

Sign up to request clarification or add additional context in comments.

Comments

0

The key code of your Remove_Item() function is wrong. You seem to have become confused by the number of levels of indirection.

Consider:

    for(i=0 ; i<size-1 ; i++)
    {
        *((*list)[i]) = *((*list)[i+1]);
    }

You've passed a pointer to your list into the function as list. *list is therefore the pointed-to list, and (*list)[i] is its ith element. You mean to replace each ith element with the i+1th, or that's what you should mean to do, but what actually do is change the value pointed to by each ith element to be that pointed to by the i+1th.

Furthermore, the subsequent reallocation is clearly wrong, as has been observed in comments. You appear to want something more like this:

    for (i = 0; i < size - 1; i++)
    {
        (*list)[i] = (*list)[i + 1];
    }
    *list = realloc(*list, sizeof(**list) * (size - 1));

Note also that this sets *list to NULL and leaks memory if realloc() fails. The fact that you are reallocating to a smaller size does not guarantee that realloc() will succeed.

And any way around you leak the memory that the removed list item pointed to.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.