How to create an array on 100 elements in C that would have element indices from -77 to 22.
1 answer
For example, take an ordinary array of 100 elements, and use a pointer to the 77th element.
int vanilla_array[100]; int* shifted_array = &vanilla_array[77]; Now you can use shifted_array with indices from -77 to 22 .
- Hats off ... :) - Harry
- This task was set by the teacher at the institute. This option with a pointer to the 77 element was proposed to him immediately. But it did not suit him, t to further goes to the elements through the pointer, and not through the name of the array. Requires that the array indices are from -77. - J.Bon
- Well, pass this pointer to a function with the signature
void f(int[] data). Hope this will convince your teacher. Unless he wants from you any idiotic solution, which is according to the standard undefined behavior (this is quite likely if the teacher is “old-school”). - VladD - Option with the pointer passed. The teacher wanted me to make sure that there is no other way or he does not provide a stable solution to the problem. Thanks for the help. - J.Bon Nov.
- @ J.Bon: Oh :) So, there was a trap. That's good. - VladD
|