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

Advertisement
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:

 

 

DYNAMICA.C

 

 

 

#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);
  getch();
  return 0;
}

Output will be displayed as:

DYNAMICA.EXE

Download Source Code:

Advertisements

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
 
 
Comments:10
adrios
October 24, 2011
not sure if serious

this is not dynamic array :|
surendra
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.
sruthi
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
coding

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

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

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

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

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
DMCA.com