Products
Services
Contact Us

Scripts: Cplus :: C++ arrays :: Library Article #1

Developer's Section

Create an Array in C++
By: Erobo Team Member

Hire a Developer for Related Work / Installation | $55 hr
Rating:  | Rate It:   
Average Votes: (2252)
Favorites:

Learn to create and populate a C++ array.

In this tutorial we will look at the C++ arrays.

Let's begin by creating a static array. Static arrays are the ones that are not resizable.

 Code Snippet 1


#include <stdio.h>

//Allocate a static array of 8 slots
//Array of Type int

  int main() {

  int myArray[8]; 


  myArray[0]=1892874;
  myArray[1]=239583;
  myArray[2]=2453;
  myArray[3]=256747;
  myArray[4]=6834838;
  myArray[5]=85954836;
  myArray[6]=85737;
  myArray[7]=7403853;

  }


Dynamic arrays are declared using pointers.

 Code Snippet 2

#include <stdio.h>

//Allocate a dynamic resizable array of 8 slots
//Array of Type int

  int main() {

  //Declare array using a pointer to memory
  int *myArray; 
   
  myArray = new int[8]; 

  myArray[0]=1892874;
  myArray[1]=239583;
  myArray[2]=2453;
  myArray[3]=256747;
  myArray[4]=6834838;
  myArray[5]=85954836;
  myArray[6]=85737;
  myArray[7]=7403853;

  //resize it

  myArray = new int[9];

  //populate

  for(int i=0; i< sizeof(myArray); i++) {
myArray[i] = i * 965 -34;
  }

  }


See other Scripts in C++ arrays

Submit Your Scripts:


System Message:
  • Your C++ script has been sent. Please allow 48 hours for processing.


If you would like to have your C++ scripts published in this section please fill out
the form below:
*Your Name or Username:
Home Town:
*Email:
*Description and Code:
*Enter Code shown
to the right:

[ Refresh Image ]