Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

C for Beginner -- Part7: C Data Storage--Array

c c for beginner data storage data structure array

  • Please log in to reply
No replies to this topic

#1 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 19 March 2013 - 01:24 AM

From this series tutorial  we had come to know about c basics , some important weapons already. Today we will learn about the most important data storage -- Array.

 

 

What is Array?

Array is simply a collection of same type variables. An array is a collection of same type of elements which are sheltered under a common name. An array can be visualized as a row in a table, whose each successive block can be thought of as memory bytes containing one element. Array holds successive memory blocks in the primary memory. The basic structure of an array declaration

<type-of-array> <name-of-array> [<number of elements in array>];

 

Lets say we have to store a name for programming purpose. We can start like this 

char a,b,c,d,e,f,g,h,i,j;
a='k';
b='e';
c='r';
d='n';
e='e';
f='l';
g='c';
h='o';
i='d';
j='e';
k='r';
printf("%c%c%c%c%c%c%c%c%c%c%c",a,b,c,d,e,f,g,h,i,j,k);//will show kernelcoder

This is not a good idea to store each character in separate variable. Thus the idea of array comes in. The name 'kernelcoder' has 11 characters. We can start storing the name kernelcoder in an array like this

char name[12] = "kernelcoder";
printf("My name is %s",name);//%s is used for string(collection of char data)

The name[12] is the declaration of an array which can hold 12 variables named name[0],name[1],.......,name[11]. Here 'name' is the array name, in the third bracket the 12 defines how many variables should the array have, this is called index of an array. See the length of 'kernelcoder' is 11 but we used 12, because for character array a null('\0') character is appended to the array thus this array has an index of 12. Basically a string is ended with a null character(\0).

chararray.png

 

 

Types of Array

We can have all types of arrays as we knew for datatypes. So we can use character array(sometimes called as  string), integer array, floating data array etc. We just need to know how to declare and use them in our program. For an integer array roll[10], the array can hold 10 roll numbers (not 10 digits).

int roll[10];
roll[0] = 1;
roll[1] = 2;

roll[2] = 3;

roll[3] = 4;

roll[4] = 5;

roll[5] = 6;

roll[6] = 7;

roll[7] = 8;

roll[8] = 9;

roll[9] = 10;
printf("\nroll[9]=%d",roll[9]);

Attached Thumbnails

  • char2darray.png
  • intarray.png
  • error1.png
  • error2.png

  • 1





Also tagged with one or more of these keywords: c, c for beginner, data storage, data structure, array