Mp. Roe
Mp. Roe

Reputation: 23

Allocate in C specific index range

Is it possible to allocate specific index range in C , like they do in fortran :

allocate(f(istart-1:iend+1), u(istart-1:iend+1), uold(istart-1:iend+1))


do i = istart, iend
.
.
.
end do

Upvotes: 0

Views: 525

Answers (3)

Paul Ogilvie
Paul Ogilvie

Reputation: 25286

What you want basically boils down to setting the lower bound and upper bound of an array, as in Basic (and apparaently Fortran): Dim Matrix(3, 4) As Integer.

As a use I can think of processing a part in a sparse array without having to allocate the full array.

In C you can achieve this by using an integer variable that serves as an offset, as a trivial example:

int getElement(int *arr, int index, int offset)
{
    return *(arr + (index-offset));
}

Also more elaborate methods are possible, e.g.:

typedef struct MY_ARR {
    int *arr;
    int lowerbound;  // from
    int upperbound;  // up and including
} t_myArr;

t_myArr *newArr(int lower, int upper)
{
    t_myArr *p;
    if (!(p= malloc(sizeof(t_myArr))) return 0;
    if (!(p->arr= calloc(sizeof(int),upper-lower+1))) {free(p); return 0;}
    p->arr.lowerbound= lower;
    p->arr.upperbound= upper;
    return p;
}
int getElement(t_myArr *p, int element)
{
    if (!p || p->arr.lowerbound<element || element>p->arr.uperbound) return 0;
    return(*(p->arr+element-lowerbound));
}

In essence you have now created your own integer array type.

Upvotes: 1

Serge Ballesta
Serge Ballesta

Reputation: 148890

You cannot directly, and it makes no sense in C because a array is a sequence of consecutive elements and the first element has index 0 by definition. And in fact the C standard says it explicitely:

6.5.2.1 Array subscripting
...
2 A postfix expression followed by an expression in square brackets [] is a subscripted designation of an element of an array object. The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2))). Because of the conversion rules that apply to the binary + operator, if E1 is an array object (equivalently, a pointer to the initial element of an array object) and E2 is an integer, E1[E2] designates the E2-th element of E1 (counting from zero).

But you can easily simulate it by creating a new pointer with a specific offset. Assuming that you want an array accepting indexes for start (inclusive) to end (exclusive) (start and end being 2 integer values with end > start):

int * arr = malloc((end - start) * sizeof(int));
arr -= start; // must be done in two steps because malloc return a void*
              // and no pointer arithmetics is possible on void *;

for (int i=start; i<end; i++) {
    arr[i] = ...;
    ...
}
...
free(arr + start);  // do not forget the offset when freeing...

By definition, arr[start] = *(arr + start) = *(arr_orig - start + start) = *arr_orig

Upvotes: 1

Jabberwocky
Jabberwocky

Reputation: 50775

Maybe you want something like this:

int array[100];

#define Array(index) array[(index) - 10]

int main() {

  Array(10) = 1;  // actually the same as array[0];
  Array(11) = 2;  // actually the same as array[1];

  printf("%d\n", Array(10) + Array(11));

}

This is really a hack and I'd not advise using it actually.

Upvotes: 0

Related Questions