0
#include <stdio.h>
#include <conio.h>

int main() {
    int arr[] = {1,2,3};
    arr[3] = 4;
    printf("%d", sizeof(arr) / sizeof(1));  // To print the length of arr
    getch();
    return 0;
}

I first created this array of length 3 and then at index 3 I put 4 as its value. So the length should be 4 now, but still, after compiling it is printing 3. Why?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Faiyaz Ahmad
  • 87
  • 1
  • 6
  • 7
    Arrays can't change their lengths. Ever. – user253751 Jan 15 '21 at 14:11
  • 1
    [Undefined, unspecified and implementation-defined behavior](https://stackoverflow.com/q/2397984) – 001 Jan 15 '21 at 14:13
  • 4
    No you didn't change the length, instead `arr[3] = 4;` access the array one beyond the end of the array, which is undefined behavior. – costaparas Jan 15 '21 at 14:13
  • Please compile you're code with warnings enabled, if using `gcc`/`clang`, consider at the least the options `-Wall -Werror -Wextra`. Consider also, using [`valgrind`](https://valgrind.org/) to help diagnose runtime errors like this. – costaparas Jan 15 '21 at 14:14
  • Unlike some other languages, C does not automatically resize arrays. – Weather Vane Jan 15 '21 at 14:17

3 Answers3

1

Arrays in C do not automatically extend themselves if you index outside the defined range. When you write

arr[3] = 4;

you are writing outside the bounds of the array.

The problem is that C does not require any kinds of bounds checking on array accesses and places no requirement on the compiler or runtime environment to handle out-of-bounds indexing in any particular way - the behavior is undefined. Any result is possible, including appearing to work as expected. You could also overwrite another variable. You could corrupt the stack frame leading to a crash later.

An array's size is fixed over its lifetime. You can allocate space dynamically with malloc or calloc and resize it using the realloc function, but again that doesn't happen automatically, you have to write the code to keep track of the current size and resize as necessary.

John Bode
  • 119,563
  • 19
  • 122
  • 198
0

You cannot create an array and then try to resize it. Arrays have a fixed size. If you want to store data dynamically, that is, with variable size, you need to use data structure.

  • It's still called "array", not "arrangement". Also, "static" has a specific meaning in the C language that is different from what you meant here. Be careful not to confuse these terms. – mkrieger1 Jan 15 '21 at 14:19
  • I am not completely conversant with the English language, I just use the translator. I understand that the terms were misused, my mistake. – Willian Pinheiro Jan 15 '21 at 15:11
0

Why is the length of the array 3 ...

Because you defined it like that here

int arr[] = {1,2,3};

Using [] and three initialisers, size 3, not going to change.

... when I have added 4 elements?

You did not add 4 elements, you created it with initially three elements, they were not added.

You did not even add a single element later.

In this code

arr[3] = 4;

you wrote a value to a memory location just behind the array. The array is not changed by that, especially not its size. Doing this causes Undefined Behaviour by the way.

C is like that, it allows you to do all kind of unwise things, without kindly telling you.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54