>is there any other way i can do this without using that goto statement?
I would do it like this.
Code:
#include <stdio.h>
int main(void)
{
int i, j, index[] = {1,4}, array[] = {10,20,30,40,50,60};
for ( i = 0; i < sizeof(array)/sizeof(*array); ++i )
{
for ( j = 0; j < sizeof(index)/sizeof(*index); ++j )
{
if ( i == index[j] )
{
break;
}
}
if ( j == sizeof(index)/sizeof(*index) )
{
printf("array[%d] = %d\n", i, array[i]);
}
}
return 0;
}
A functionized version might be like this.
Code:
#include <stdio.h>
/*
* Copy from 'array' to 'result' except for value of 'i' that match any of
* the values in 'index'.
*/
int foo(const int *array, int asize, const int *index, int isize, int *result)
{
int i, j, rsize = 0;
for ( i = 0; i < asize; ++i )
{
for ( j = 0; j < isize; ++j )
{
if ( i == index[j] )
{
break;
}
}
if ( j == isize )
{
result[rsize++] = array[i];
}
}
return rsize;
}
/*
* Print the 'result' array.
*/
void bar(const int *array, int asize, const int *index, int isize, int *result)
{
int i, rsize = foo(array, asize, index, isize, result);
for ( i = 0; i < rsize; ++i )
{
printf("result[%d] = %d\n", i, result[i]);
}
}
int main(void)
{
int index[] = {1,4}, array[] = {10,20,30,40,50,60};
int result[ sizeof(array)/sizeof(*array) ];
bar(array, sizeof(array)/sizeof(*array),
index, sizeof(index)/sizeof(*index),
result);
return 0;
}
/* my output
array[0] = 10
array[2] = 30
array[3] = 40
array[5] = 60
*/
Going through the other responses, I don't think I quite understand what you want to do (I think I need more caffeine to start my day). So this is here for whatever it may be worth.