This programm ask the user to fill a table and ask the value he want to delete then make the new table without the value
#include<stdio.h>
#include<stdlib.h>
main()
{
int *tab;
int *p1,*p2;
int nb,val,i;
printf("Number of values for the table ? ");
scanf("%d",&nb);
tab=(int*)malloc(nb*sizeof(int)); // on alloue le nombre de cases demandees au tableau
for (i=0;i<nb;i++)
{
printf("value n %d of the table? ",i);
scanf("%d",&tab[i]);
}
printf("\n value to delete of the table ? ");
scanf("%d",&val);
p1=tab; // on fait pointer p1 s ur la premiere case du tableau
p2=tab; // idem pour p2
// p1 va parcourir le tableau tout entier
// p2 n'avancera que si l'element du tableau en cours est different de val
while (p1<tab+nb) // tant qu'on n'est pas a la fin du tableau
{
if (*p1!=val) // si l'element en cours est different de val
{
*p2=*p1; // on recopie l'element dans la case pointee par p2
p2++; // on incremente p2 pour passer a la case suivante
}
p1++;
}
for (i=0;i<*p2-*tab;i++) // p2-tab= nombre de cases encore remplies
{
printf("value n %d of the table : %d\n",i,tab[i]);
}
}
I don't understand why the last for loop can be write like this as well ?
for (i=0;i<p2-tab;i++)
it should not return the value of the address, only the address