why can't we change the default starting index of an array to 1 instead of 0 ?
and another question how much fields can a bit field have ay most ?
thanks previously

Dani AI

Generated

Short answer: you can’t change C’s array origin to 1 because that is a language/ABI decision tied to how C models arrays in memory. and were right in spirit — the choice is historical and deliberate, not a simple compiler knob to flip. (cs.utexas.edu)

Why that matters: C treats an array as a contiguous block of storage and an index is used as an offset from the block’s start. Making the first element “0 elements away” keeps the address math simple and efficient; changing the origin would force either extra work at every access or a different memory model. This is why most systems‑level languages that expose memory directly picked zero as the index origin. (en.wikipedia.org)

If you really want 1‑based indexing in C there are practical workarounds: write thin access functions/macros that subtract 1, or allocate one extra element and hand out a pointer adjusted so callers can use 1..N indices. Both are idioms you’ll see, but they add maintenance cost and you must obey C’s pointer arithmetic rules (one‑past‑end is special; pointers before the start are not allowed), so be careful to avoid UB. If one‑based indexing is a core requirement, choose a language that supports it natively instead of fighting the language. (oifans.cn)

About bit‑fields: the C standard does not give a single “maximum number of fields.” Each bit‑field’s width must be a nonnegative integer constant not exceeding the width of the underlying object representation for that type, and placement/packing/order/signedness are implementation‑defined. A zero‑width unnamed bit‑field forces alignment to the next allocation unit. For portable, predictable packing (wire formats, cross‑compiler structures) prefer explicit masks and shifts or fixed‑width integer layouts rather than relying on compiler bit‑field packing. (oifans.cn)

Summary: accept the language model when working in C; if you need different indexing or deterministic packing, either wrap/offset carefully or use a language/approach that guarantees the semantics you need.

Recommended Answers

All 4 Replies

This is a common question for a new C or other language programmer. To know why I guess you could find books on the history of C and how it came to be. But it's mostly historic and you can code as you see fit. However, it is best to not fight the tide this time and learn how to code using the standard calls and accept that arrays and more start at zero.

As to the bit field question, that varies with the storage you chose. For Integer Types (noted at https://www.tutorialspoint.com/cprogramming/c_data_types.htm ) you have 1 to 4 bytes and since a byte (for most machines) is 8 bits that would be 8 fields per byte.

This can be considered arcana today by some. Let's say you are programming on an average PC today. Not much reason to encode data into bit fields. Just use variables to clearly state what you mean. In embedded programming bit fields are not going away. Some will open a debate on this. I think this quote applies here.

This has made a lot of people very angry and been widely regarded as a bad move.

Douglas Adams

Going back to Kernighan & Ritchie...

An array name is a pointer expression ... a reference to an array is converted by the compiler to a pointer to the beginning of the array ... a reference to a[i] can also be written as *(a+i)

Obviously this implies that the first element of an array MUST be [0]

Thanks for the clearance i see now so it's just all about the makers and that's that, just seems we should use it the way it was invented to be heh.

You could always switch to a language where indexing starts at 1. FORTRAN, Lua, Pascal and Smalltalk to name a few.

commented: Oh Pascal, where I spent too much time. +12
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.