DEV Community

Cover image for How To Increase The Size Of An Array In C ?
Gaurav
Gaurav

Posted on • Updated on

How To Increase The Size Of An Array In C ?

To understand this better let's see how you would resize an array in JavaScript with a simple example.



let someArray = [1,2,3,4,5];
console.log(someArray);
someArray.length +=5;
console.log(someArray.length);
console.log(someArray);


Enter fullscreen mode Exit fullscreen mode

Which when executed gives this output:
Output of array

This was a straight forward and easy example of how you would do that in JavaScript but this is not the case with some other languages like C.

So how would you do that in C?
I'm glad you asked. But first we need to understand what the problem is or why isn't it this simple in C.

In C we need to understand that memory for an array is allocated at compile time and not at runtime which means we cannot increase it once the array size is declared in the code.
We know to create an array we need a contiguous memory allocation or in simpler terms the next element in an array must be placed adjacent or after the previous one. After specifying the size of an array we can't determine if the memory location adjacent to the last element in an array is empty or not and hence we can't increase the size of the array.I hope that made sense. Now you might be thinking why were we able to do this in JavaScript, then the simple answer is JavaScript arrays are just objects with special properties. JavaScript is a wired language and I love it.

Steps to get the job done:
1.Create an array (lets say p) with n items.
2.Create another array (lets say q) but an empty one which is larger than array p.
3.Now copy the elements of p into q by a simple for loop.
4.Delete the memory held by pointer p using free(p); so that array p no longer exists.
5.Assign the address of array q in p.
6.Assign q the value NULL so that it can't access array q.
7.And that's it. The array size is now increased.

Please read the code for better understanding.
Code



#include<stdio.h>
#include<stdlib.h>
int main(){
//Two pointers for two different arrays
    int *p;
    int *q;

//declaring array at pointer p
    p = (int *)malloc(5*sizeof(int));
    p[0]=1;
    p[1]=3;
    p[2]=5;
    p[3]=7;
    p[4]=9;
//Printing the elements of p
    printf("Array p: \n");
    for(int i=0;i<5;i++){
        printf("%d \n",p[i]);
    }
//declaring array at pointer q
    q=(int *)malloc(7*sizeof(int));
    for(int i=0;i<5;i++){
        q[i]=p[i];//assigning elements of p to q
    }

    free(p);//releasing the memory held by pointer p

    p=q; //assigning the address held by q to p for the array
    q=NULL; //removing the address of array from q

//printing the elements of p
    printf("Array q converted to p: \n");
    for(int i=0;i<7;i++){
        printf("%d \n",p[i]);
    }

    return 0;
}



Enter fullscreen mode Exit fullscreen mode

Output
output of c
So we can clearly see that the array p in the beginning was of size 5 but after all that code its increased to 7.
The 0 at the end of the array after size increase is added by C automatically if no data is entered at that index.

Let's understand with this example.
Let a guy named P has a car (array 1). But now he has kids due to which he needs a bigger car. He has a friend named Q who is moving to a different country and wants to sell his car (array 2). P realizes that his friend's car will be just the perfect size for him so he makes his friend Q an offer and buys his car (array2) and Q leaves the country q=NULL;. Now P has 2 cars and since he doesn't need his old car (array 1) he gets rid of it free(p);.

I hope this was clear and easy to understand. Thanks for your time.

Top comments (2)

Collapse
 
pauljlucas profile image
Paul J. Lucas

man realloc

That's a lot simpler.

Collapse
 
crazysamurai profile image
Gaurav • Edited

indeed, but this is just to show an algorithmic approach.