Home > Mobile >  How to Called Resetting Array Index to Beginning
How to Called Resetting Array Index to Beginning

Time:06-07

How can we called resetting array index to beginning for clearing that array? I mean we are not clearing that array by setting 0 (zero).

When we reset the array index to beginning of that array, when new data come, we will write to old data which is located in the beginning of the array. What is the name of this type of resetting in terminology?

Best regards, Mert.

CodePudding user response:

Arrays in C are not “reset.” An array in C is simply memory that can hold values. (Specifically contiguous memory for elements of a particular type.) It does not have any extra data about which elements have been set or reset or which are in use or not. The only “clearing” operation for an array is to set its elements to the value zero, or to some other value you might choose to have “cleared” meaning for you.

Just as an array does not have any extra data about which elements have been set or reset, it does not have any index associated with it. To index an array, you maintain your own variables containing whatever indices you want. If you want to track how many elements of an array are in use by your program, you make your own variable to hold that number. (Novice C programmers often use an int variable for that. Experienced programmers may prefer the unsigned type size_t or the signed type ptrdiff_t, both of which are declared in <stddef.h>.)

To reset your index to the beginning of the array, you simply set your index to zero.

CodePudding user response:

'initialisation' would be the term for the initial value of array elements, so 'reinitialisation' might be a reasonable term, or perhaps 'overwritting'.

We should also note that allocated, initialised and 'occupied' aren't always the same thing.

int a[10];

Allocated, not explicitly initialised. Likely not occupied (no element has been assigned a known, meaningful value).

int a[10] = { 0 };

Allocated, explicitly initialised, maybe partly/fully occupied. Note that only the first element is explictly initialised and it depends on the C standard you are working too as to whether elements 1-9 are also initialised with the value 0.

int a[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

Allocated, explicitly initialised, fully occupied.

What does 'occupied' mean?

Whilst an array can be initialised, whereby all elements have a known initial value, the concept of occupied is likely to be application dependent. It might be necessary to maintain a separate 'count' that determines how many elements of the array actually contain useful application data, and it might be necessary to maintain that value independent of an accessor index.

int a[10] = { 0 };
int occupied = 0;
int index;

for (index = 0 ; index < 10 ; index  ) {
  a[index] = index;
  occupied  ;
}

Accessing a value beyond the count of occupied values is likely to be undesirable.

Note that elements do not 'need' to be occupied sequentially from any position in the array, although it is common. Sometimes the element value indicates whether it is valid, and this is why initialisation is important. Uninitialised values are likely to lead to unintended behaviour.

  • Related