"C and Pointers" chapter 8th programming exercises 7th:
1 /*2 * * 2014/12/09, by Zhm3 */4#include <stdio.h>5#include <stdarg.h>6 7 /*8 * * Function function:9 * * Allows users to access "pseudo-array", its subscript can be arbitrarily specified, and have a complete error checkTen * * This pseudo-array is stored in column main order, and the leftmost subscript changes first. One * * Function Parameters: A * * ArrayInfo: A variable-length integer array that contains information about the number of pseudo-array dimensions, - * * Arrayinfo[0] Specifies the number of dimensions of the pseudo-array, 1<=arrayinfo[0]<=10 - * * arrayinfo[1] and arrayinfo[2] give the upper and lower bounds of the 1th dimension, etc. the * * ...: variable parameter list containing parameters equal to the number of dimensions of the pseudo-array. - * * Subscript value used to identify a specific position in a pseudo-array - The * * function returns: - * * Returns the offset amount of a pseudo-array, used to denote the subscript of a vector (one array) + * * Returns 1 if the following error condition occurs - * * 1. The dimension is not between 1 and 10; 2. Subscript is less than the lower limit value; + * * 3. The subscript is greater than the upper limit, 4. The upper value is less than the corresponding lower limit value. A */ at int -Array_offset2 (intarrayinfo[], ...) - { - /* - * * Check if the dimension of the pseudo-array is between 1-10 - */ in intN = arrayinfo[0];//n Indicates the number of dimensions of the pseudo-array - if(N <1|| N >Ten ) to return-1; + - va_list Var_arg; the intS//s = subscript parameter * intLo, hi;//Lo represents the lower bound, HI represents the upper limit $ intLo_index, Hi_index;//The index value of lo and hi in the arrayinfo arrayPanax Notoginseng intLOC =0;//represents the target location of a pseudo-array - intJ; the intSize_mutiply =1; + A /* the * * The target position of the pseudo-array is represented by an integer offset from the starting position of the pseudo -array + * * The following section calculates the target location of a pseudo-array by formula - */ $Va_start (Var_arg, arrayinfo[0] ); $ - for(j =0, Lo_index =1, Hi_index =2; J < N; + + J, Lo_index + =2, Hi_index + =2 ) - { thes = Va_arg (Var_arg,int);//accessing the mutable parameter list with the VA_ parameter macro -Lo =Arrayinfo[lo_index];WuyiHi =Arrayinfo[hi_index]; the - //determine if there is an error Wu if(Lo > Hi | | s < LO | | s >hi) - return-1; About $ //Calculate target Location -Loc + = (s-lo) *size_mutiply; - -Size_mutiply *= (Hi-lo +1 ); A } + va_end (var_arg); the returnLoc; - } $ the int the Main () the { the intArrayinfo[] = {3,4,6,1,5, -3,3 }; - intA, B, C; inscanf"%d%d%d", &a, &b, &c); the intoffset =Array_offset2 (ArrayInfo, A, b, c); theprintf"%d", offset); About return 0; the}
Pseudo-array (stored in column main order) subscript access