How To Increase The Size Of An Array In C ? - DEV Community ‍ ‍

Add reaction Like Unicorn Exploding Head Raised Hands Fire Jump to Comments Save Boost More... Copy link Copy link Copied to Clipboard Share to X Share to LinkedIn Share to Facebook Share to Mastodon Share Post via... Report Abuse

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.

pic Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Submit Preview Dismiss Collapse Expand pauljlucas profile image Paul J. Lucas Paul J. Lucas 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
Sep 9 '21 Dropdown menu
  • Copy link
  • Hide

man realloc

That's a lot simpler.

Collapse Expand crazysamurai profile image Gaurav Gaurav 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
Sep 10 '21 • Edited on Sep 18 • Edited Dropdown menu
  • Copy link
  • Hide

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

Code of Conduct Report abuse

Are 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
Data Structures : Graph Traversal (DFS) #algorithms #java #beginners #programming Data Structures : Introduction to Graphs #programming #beginners #algorithms The This Keyword in JavaScript #javascript #beginners #webdev #programming
DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

Tag » How To Double The Size Of An Array In C