这是使用 Maroun 技巧的代码片段。
#include<stdio.h>
void print_array(int *array);
void shift_array_normal(int *array,int arrayLen);
int main(void)
{
int array[]= {-1,32,44,185,28,256,22,50};
array[0] = sizeof(array) / sizeof(array[0]) - 1;
print_array(array);
return 0;
}
void print_array(int *array){
int index,arrayLen = array[0];
//length of array is stored in arrayLen now we can convert array back.
printf("Length of array is : %d\n",arrayLen);
//convert array back to normal.
shift_array_normal(array,arrayLen);
//print int array .
for(index = 0; index < arrayLen; index++)
printf("array[%d] = %d\n",index,array[index]);
}
/*removing length element from array and converting it back to normal array*/
void shift_array_normal(int *array,int arrayLen){
int index;
for(index = 0; index < arrayLen; index++)
array[index] = array[index + 1];
}