SCSI Hard Drives
Search
Advanced Search

Categories


Recently Viewed
Pages



My Links
Web Directory Index
A human edited, comprehensive web directory list.
Link Exchange
DesignFirms Link Exchange


Storing strings in arrays in C

By : sterex
Rating : Average Rating : 4.75 From 4 Voter(s)


Unlike web languages like PHP, ASP, Perl, you are NOT able to store text strings in one-dimensional arrays like you would in those web languages. C++ requires the use of 2-dimensional arrays in order to store lines of text. For this to happen C++ uses a mix of character sequences and arrays. As for a limit on how many dimensions of arrays you can have... There's an unlimited amount.

So to start off, we'll use a basic C++ format. Using the input-output stream file, the standard library file, and the strings file.



#include <iostream>
#include <stdlib.h>
#include <string.h>


using namespace std;


int main(){


cin.get();
return 0;
}


Now we'll start off by calling some new variables, as well as a new character array.



#include <iostream>
#include <stdlib.h>
#include <string.h>


using namespace std;


const int num_arrays = 10;
const int array_length = 99;


char string_array[num_arrays][array_length];


int main(){


cin.get();
return 0;
}


How a two-dimensional array works is that it has two certain aspects to it. The first is how many arrays it can hold. The second is how many characters it can carry in this instance. We used the integer num_arrays to set how many arrays the array can hold. array_length is for how many characters one array can hold up to. Both these values are interchangeable.

Now all that remains now is to set character values into the array.

Since we don't want to numerically set each array one by one, we'll use the array listing.



#include <iostream>
#include <stdlib.h>
#include <string.h>


using namespace std;


const int num_arrays = 10;
const int array_length = 99;


char string_array[num_arrays][array_length] = {
             "Array 0",
             "Array 1",
             "Array 2",
             "Array 3",
             "Array 4",
             "Array 5",
             "Array 6",
             "Array 7",
             "Array 8",
             "Array 9"
             };


int main(){


cin.get();
return 0;
}


There is one thing you should know about arrays. The array value will begin at 0, but, it will always need one space for one last value, /0. /0 tells the array to stop storing values. It is automatically placed at the end of calling arrays for values. If we set an array value for 10, it would mean it has 11 arrays to store data. But since /0 is needed, we can only use 10. Be warned though, if you call array value 10, nothing will appear.

Since all the values are set into the array, we should now call them. We can call them one by one with C-output, but let's just use a loop to see it in action.



#include <iostream>
#include <stdlib.h>
#include <string.h>


using namespace std;


const int num_arrays = 10;
const int array_length = 99;


char string_array[num_arrays][array_length] = {
             "Array 0",
             "Array 1",
             "Array 2",
             "Array 3",
             "Array 4",
             "Array 5",
             "Array 6",
             "Array 7",
             "Array 8",
             "Array 9"
             };


int main(){
   
    for(int count = 0; count < num_arrays; count++)
    {
              cout << string_array[count] << endl;
             
              }


cin.get();
return 0;
}


After it compiles you should see all your array values pop up.








Comments / Feedback

James Email
January 1, 2008, 7:08 pm

Thanks
RSS 2.0: Syndicate this article

Add Comment
* Name


* Email Address


Site



*Image Validation (?)


*Comments / Feedback





Print Article Print Article Send to a friend Send to a friend Save as PDF Save as PDF Social Bookmarking
Add to: Mr. Wong Add to: Webnews Add to: Icio Add to: Oneview Add to: Folkd Add to: Yigg Add to: Linkarena Add to: Digg Add to: Del.icio.us Add to: Reddit Add to: Simpy Add to: StumbleUpon Add to: Slashdot Add to: Netscape Add to: Furl Add to: Yahoo Add to: Spurl Add to: Google Add to: Blinklist Add to: Blogmarks Add to: Diigo Add to: Technorati Add to: Newsvine Add to: Blinkbits Add to: Ma.Gnolia Add to: Smarking Add to: Netvouz Information
Rate this Article :

1

2

3

4

5

6

7

8

9

10
Poor Excellent