tags:

views:

114

answers:

5

hi, so far, i m working on the array with 0th location but m in need to change it from 0 to 1 such that if earlier it started for 0 to n-1 then now it should start form 1 to n. is there any way out to resolve this problem?

+6  A: 

C arrays are zero-based and always will be. I strongly suggest sticking with that convention. If you really need to treat the first element as having index 1 instead of 0, you can wrap accesses to that array in a function that does the translation for you.

Why do you need to do this? What problem are you trying to solve?

Chris Schmich
Out of curiosity. Anyways, with the help of function how to do this?I want to know, if you can provide any help.
suvirai
If you are genuinely curious regarding array indexing you can look at FORTRAN, which does allow you to change your indexing range.`DIMENSION MYARRAY(-5:5)`The array MYARRAY could then be indexed into using indexes -5 to 5 (inclusive 0).
linuxuser27
Didier's suggestion below is clever, otherwise you could do something like: `int GetElement(int xs[], int index) { return xs[index - 1]; }` and then access the first element with `GetElement(xs, 1);`
Chris Schmich
QBASIC had OPTION BASE (0|1) to set the default start index
mikek3332002
@mikek: Perl has an option to change from zero-based to one-based arrays. You are always counselled against using it, though.
Jonathan Leffler
+5  A: 

Array indexing starts at zero in C; you cannot change that.

If you've specific requirements/design scenarios that makes sense to start indexes at one, declare the array to be of length n + 1, and just don't use the zeroth position.

Amarghosh
+1  A: 

You can't.

Well in fact you can, but you have to tweak a bit. Define an array, and then use a pointer to before the first element. Then you can use indexes 1 to n from this pointer.

int array[12];
int *array_starts_at_one = &array[-1]; // Don't use index 0 on this one
array_starts_at_one[1] = 1;
array_starts_at_one[12] = 12;

But I would advise against doing this.

Didier Trosset
I think this leads to undefined behavior.
GMan
Yep, by C99 §6.5.6/8, you aren't allowed to form an address outside an array (except one past the end) by adding to an address inside the array.
Potatoswatter
+2  A: 

Subtract 1 from the index every time you access the array to achieve "fake 1-based" indexing.

If you want to change the numbering while the program is running, you're asking for something more than just a regular array. If things only ever shift by one position, then allocate (n+1) slots and use a pointer into the array.

enum { array_size = 1000 };

int padded_array[ array_size + 1 ];
int *shiftable_array = padded_array; /* define pointer */

shiftable_array[3] = 5; /* pointer can be used as array */
some_function( shiftable_array );

/* now we want to renumber so element 1 is the new element 0 */
++ shiftable_array; /* accomplished by altering the pointer */

some_function( shiftable_array ); /* function uses new numbering */

If the shift-by-one operation is repeated indefinitely, you might need to implement a circular buffer.

Potatoswatter
A: 

Some more arguments why arrays are zero based can be found here. Infact its one of the very important and good features of the C programming language. However you can implement a array and start indexing from 1, but that will really take a lot of effort to keep track off.

Say you declare a integer array

int a[10];
for(i=1;i<10;i++)
a[i]=i*i;

You need to access all arrays with the index 1. Ofcourse you need to declare with the size (REQUIRED_SIZE_NORMALLY+1). You should also note here that you can still access the a[0] element but you have to ignore it from your head and your code to achieve what you want to.

Another problem would be for the person reading your code. He would go nuts trying to figure out why did the numbering start from 1 and was the 0th index used for some hidden purpose which unfortunately he is unaware of.

Praveen S