Home > Software engineering >  Great god answer of 2 d dynamic array, how to perfect removes a (line) element from an array and rel
Great god answer of 2 d dynamic array, how to perfect removes a (line) element from an array and rel

Time:10-04


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 array

CodePudding user response:

refer to the second floor shenyi0106 response:
, after deleting the current array index, and since this index loops going backwards, in turn forward (ch=ch [I - 1] [I]) an element, the ch [I]=NULL,

The difficulty of the students, it is not hard to
should not be the last one array is empty? Why is to remove a bit empty?

CodePudding user response:

Put the array pointer points to control release such as delete, and then to delete the array elements

CodePudding user response:

Set a breakpoint step through and see whether according to your way of thinking

CodePudding user response:

Actually computer after each byte of physical memory that are read/write, never because of the so-called new, delete or malloc, free and is created and destroyed, the difference only lies in the operating system memory management module can find when you read and write and whether to take appropriate action, and the granularity of the operating system manages memory rather than bytes page, a page is usually 4 KB,
  • Related