How To Increase The Size Of An Array In C ? - DEV Community
Maybe your like
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 modeWhich when executed gives this output: 
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 modeOutput
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.
Templates let you quickly answer FAQs or store snippets for re-use.
Submit Preview Dismiss Collapse Expand
Paul J. Lucas Follow Retired Principal Software Engineer, Instructor, Author — most recently of the book Why Learn C. - Email [email protected]
- Location San Francisco Bay Area
- Education University of Illinois at Urbana-Champaign
- Work Retired Principal Software Engineer
- Joined Jan 21, 2017
- Copy link
- Hide
man realloc
That's a lot simpler.
Collapse Expand
Gaurav Follow A student, working on web development skills, data structures and algorithms. - Email [email protected]
- Location Shimla, India
- Education Himachal Pradesh University
- Joined May 31, 2021
- Copy link
- Hide
indeed, but this is just to show an algorithmic approach.
Code of Conduct • Report abuseAre you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
Gaurav Follow A student, working on web development skills, data structures and algorithms. - Location Shimla, India
- Education Himachal Pradesh University
- Joined May 31, 2021
We're a place where coders share, stay up-to-date and grow their careers.
Log in Create accountTag » How To Double The Size Of An Array In C
-
Doubling The Array Size In C? - Stack Overflow
-
How To Increase Size Of An Array In C And C++ - Dot Net Tutorials
-
How To Double An Array Size? - C Board
-
Length Of Array In C
-
C Program: Find The Length (or Size) Of An Array - SCRIPTVERSE
-
How To Increase The Size Of An Existing Array Statically And ... - Quora
-
Do Resizable Arrays Always Double In Size When They Run Out ... - Quora
-
Array Size - MATLAB Size - MathWorks
-
Array Basics
-
C - Arrays - Tutorialspoint
-
Arrays (C++) - Microsoft Learn
-
How Do I Double The Size Of An Array In C - Anycodings
-
Array Declaration
-
Find Final Value If We Double After Every Successful Search In Array