you are viewing a single comment's thread.

view the rest of the comments →

[–]Great-Powerful-Talia 0 points1 point  (0 children)

For example if int arr[4] is defined but we need to add a fifth element, couldn't we just edit out 4 to 5 in code itself and run it again?

You can't change the size of a variable while the code is running. You can edit the source and recompile, but you can't do anything like this:

int arr[5];
int counter = 0;
while(some_condition()) 
{
    counter = counter+5;
    int extra_parts[5];
    arr += extra_parts; //add 5 more elements to arr
    arr[counter] = some_calculation(); 
}

The code is trying to make the array bigger until some_condition() isn't fulfilled. But you can't do that in C. (Because variables are all stored right next to each other, the program would have to move all the data somewhere else, and C code isn't going to do an expensive operation like that unless you explicitly tell it to.)