As shown in figure, set ch two-dimensional array, you want to delete an element of the array (should be ok); Want to delete an element, delete the position after all the elements of the forward, and release the final line element of memory, run my code will be in this case, the final location is shown as (null), want to get rid of it, and release the memory, first of all, hope to have big brand me how to put" (null) "delete, free memory second, himself a small white students, thank you all for the great spirit here,
The code is as follows:
#include
#include
Void the add (char * * ch, int s);
Void del (char * * ch, int s);
The main ()
{
Char * * ch;
Int s, l;//set the two variables for the array length
Printf (" please input the array size: ");
The scanf (" % d ", & amp; S);//input array size
Ch=(char * *) malloc (s * sizeof (char));//apply for a length of s array, with p points to the first address
Printf (" please input the value of the elements in the array: \ n ");//assignment
For (l=0; L
Ch [l]=(char *) malloc (s * sizeof (char *));
The scanf (" % s ", ch [l]);
getchar();
}
For (l=0; L
Printf (" % s \ t ", ch [l]);
}
printf("\n");
Add (ch, s);//call the add function
Printf (" new array: ");
For (l=0; L<=s; L++)//all new array element
{
Printf (" % s \ t ", ch [l]);
}
Del (ch, s);//del function called
Printf (" after removing elements of an array is: \ n ");
For (l=0; L<=s; L++)//output after deleting all the elements of
{
Printf (" % s \ t ", ch [l]);
}
return 0;
}
Void the add (char * * ch, int s)//the add function, the purpose is to increase a certain position of an element
{
int z;
int i;
Printf (" please enter the location: to join ");
The scanf (" % d ", & amp; I);
If (i<=0)
{
Printf (" illegal input, please input again ");
Add (ch, s);
}
For (z=s; Z>=I; Z -)
{
Ch [z]=ch [z - 1);
}
Ch=[I - 1] (char *) malloc (s * sizeof (char *));
Printf (" please input data to join: ");
The scanf (" % s ", ch [I - 1));
}
Void del (char * * ch, int s)//del function, the purpose is to remove a location of an element
{
int n;//set a variable for the location number
int z;
Printf (" please enter the location: to delete ");
The scanf (" % d ", & amp; N);//input position
For (z=n; z<=s; Z++)//will forward all the elements of a position after n
{
Ch=ch [z - 1] [z].
}
Ch [] s - 1=NULL;//the last one is empty
//ch/s - 1=realloc (char *) (* ch, s * sizeof (char *));
}
CodePudding user response:
Top the first, the operation of the above result is to delete the second position of x (delete 1 x is before 2, 3, delete after the result is 1 2 3(null))
CodePudding user response:
After deletion, record the current array index, and since this index loops going backwards, in turn forward (ch=ch [I - 1] [I]) an element, the final ch [I]=NULL,The difficulty of the students, it is not hard to
CodePudding user response:
Array is a complete memory blocks, can't delete a row or a column of alone, but you can apply again in a fast, eliminate should be deleted rows or columns of data, will other data copied to the new memory block; And delete the original arrayCodePudding user response: