Latest Tutorials| Questions and Answers|Ask Questions?|Site Map

Home C-tutorials C Dynamic Array

Related Tutorials


Share on Google+Share on Google+

C Dynamic Array

The dynamic array is an array data structure which can be resized during runtime which means elements can be added and removed.

C Dynamic Array


In this section, you will learn how to determine the size of a dynamic array in C.

The dynamic array is an array data structure which can be resized during runtime which means elements can be added and removed.  You can see in the given example that we prompt the user to enter the number of elements that he want to set into the array.

The expression (int*)malloc(n*sizeof (int)) stores the number of elements in the memory. The malloc is basically used for memory allocation. By using the for loop the variable i reads the elements from the memory and displays the required array.

Here is the code:







#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main() {
  int* array;
  int n, i;
  printf("Enter the number of elements: ");
  scanf("%d", &n);
  array = (int*) malloc(n*sizeof(int));
  for (i=0; i<n; i++) {
  printf("Enter number %d: ", i);
  scanf("%d", &array[i]);
  printf("\nThe Dynamic Array is: \n");
  for (i=0; i<n; i++) {
  printf("The value of %d is %d\n", i, array[i]);
  printf("Size= %d\n", i);
  return 0;

Output will be displayed as:


Download Source Code:


If you enjoyed this post then why not add us on Google+? Add us to your Circles

Liked it!  Share this Tutorial

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 5, 2009

Related Tutorials

Discuss: C Dynamic Array   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
October 24, 2011
not sure if serious

this is not dynamic array :|
November 4, 2011
c tutorial

very good explanation
Viktor Lexington
January 14, 2013
Not quite right.

This is no Array. This is a pointer. And these aren't the same.
January 16, 2012
hai buddy.................................

c language is so dificult to me so give some commands and some tips to me frds..............................
hussain sheriff
February 8, 2012

this good coding...
Arnab Das
April 5, 2012

Thanks. This program helped me for just what I needed.
May 19, 2012
doesn't work at all.

doesn't work at all.
September 24, 2012
4 gratitude

thanks for this post it was very helpful 4 me
October 27, 2012

its very very very useful to me
Joaquim Miguel
April 25, 2013
and about strutures?

imagine these struture: struct person { char name[50]; int age; int phone; } now declare it: person a[20]; but imagine that you only need 1 element, how can i do it? thanks