# Change array length

Printable View

• 11-28-2010
miroslavgojic
Change array length
On beginning I define two arrays

a[5];
b[5];

after this I need next array C

c[5];

but when I have c[5] = a[5] + b[5]

I have overflow of array C.

how can I change length of array C in loop, I don't change C if I don't have overflow
• 11-28-2010
blurx
It would be c[4] = a[4] + b[4].
• 11-28-2010
Adak
Remember that arrays in C begin with index 0, so array[5] gives you indeces 0-4 only.
• 11-28-2010
miroslavgojic
I know that a[0] is first element of array

I have need for something like this:
Code:

```a[5] = {9,9,9,9,9,9} + b[5] = {8,8,8,8,8,8} = c[5+1] = {1,8,8,8,8,8,7}```
that mean c[i+1] = a[i] + b[i]
but 1 in [i+1] is conditioned by add, it not always present
Code:

```a[5] = {4,4,4,4,4,4} + b[5] = {4,4,4,4,4,4}; = c[5] = {8,8,8,8,8,8};```
in this case I don't have need for +1 in c[i]
• 11-28-2010
grumpy
Set the length of array c to be 6 and, in cases where the length of the result is 5, make the first element zero.
• 11-28-2010
Jesius
define array c longer length.
Code:

`int c[255] = {0};`
then when you do " a[5] = {4,4,4,4,4,4}+b[5] = {4,4,4,4,4,4};"
the result put into c can be begin from 0,
like c[0] = a[4]+b[4],c[1] = a[3]+b[3]...
• 11-28-2010
CommonTater
Quote:

Originally Posted by miroslavgojic
On beginning I define two arrays

a[5];
b[5];

after this I need next array C

c[5];

but when I have c[5] = a[5] + b[5]

I have overflow of array C.

how can I change length of array C in loop, I don't change C if I don't have overflow

Are you sure you're not tripping over that old C problem where the number of elements does not equal the numbers of the elements?

Array elements always number from 0, not from 1, so a[5] has elements numbered 0 ,1 ,2, 3, 4

There is no element #5... but there is The 5th Element