A D V A N C E D     A R R A Y S

INTRODUCTION
I've gotten many questions that relate to arrays but they deal more with logic rather than the syntax aspect. So I decided to place this kind of thing under this page ... which is called *drumroll please* ADVANCED ARRAYS!

CONTENTS
Cleanliness is next to godliness. And for those of you that think I'm god then I won't dissappoint you . I made you a nifty little menu since most of the information here is rather fragmented. That's the problem when you're trying to juggle a social life, school, work, and your wife all in the same 24-hour style days .


Terminology
Glossary terms are not always consistant from tutorial to tutorial, document to document so I thought that may have caused a bit of confusion. That's why this is here. There are many different terms used along with arrays and you've probably noticed I try to stay consistent by using subscript and index variable.

Pieces of the array I denote as parts or subscripts when I am referring to a specific part. I.e. subscript 15 of array 'numbers' looks like numbers[15] in actual code. The subscript is actually the number of the piece of the array, its identification. So subscript here is 15. That's what I mean when I say subscript. Other documentation uses element for the same purpose. As one friend of mine would: *shrug* "Its all the same in Mexico...".

index is also used in the same way. And it is really the same. When we refer to an index variable we're simply saying, "A variable whose value represents the index/subscript/element of the array we wish to access". So using index in place of subscript is fine as well. I just think that subscript variable sounds funny so I didn't use it .

You must also realize by now that a string is actually a character array. That's pretty obvious because by now you should have scowered my strings section and gleaned that reiterated message over and over.

If you see any more terms that are used in conjunction with arrays in C++ then please notify me so I can incorporate those as well.

String Breakdown
I really couldn't think of any other way to title this section, sorry. But anyway, this section is for those times when you have a block of data and its all in one string (character array). And what you want is to copy pieces of this array into individual strings.

Say for example you have an 80 character string. The first 20 are a first name, the following 20 are a last name, the next one is a middle initial, and the remaining is filler. And now, we want to break these up into their respective variables: first_name, last_name, middle_initial.

So, we have this as our code for variables at this point:
char datablock[81] = "first               last                m";
char first_name[21];
char last_name[21];
char middle_initial;
Notice that middle_initial is not an array. We're only using it to store one character so it would be a complete waste to make an array. So, note to all that if its only going to store one piece then why in the frinking hole would you make it an array? I've seen this done a lot so don't do it.

I have first_name and last_name declared as 21 because I need the extra one for a NULL character. Here is an interesting tale for you. I spent a great deal of time trying to figure out why the very first character in one of my strings was being set to NULL. And do you know why? Because the string that was immediately previous to it wasn't declared long enough and in trying to put the NULL character at the end it went to the beginning of my second array. I know it sounds terribly complicated, but just remember that if you declare a string as char [21] you can never use subscript 21 or bad things can happen!

Also, I didn't fill in all 80 characters of our data block because I don't want the sucker going off the screen. And since the remaining characters (past middle initial) are not used and are filler, we don't need to initialize them anyway.

(back on track and its almost a month later ... sorry)

We have established the fact that datablock has been initialized to the values we want to extract. What we need to do now is take out those pieces. The numero uno character for the first name inside datablock starts at subscript 0. We also know that the field we want to put this data into (first_name) is 20 characters long (not counting the NULL character). In lew of this (doesn't that word sound creepy?) a memcpy() would look like so:
memcpy(first_name, datablock + 0, 20);
The " + 0" is a formality because 'datablock + 0' is the same as 'datablock'. But it brings up an important point (get to that in a sec). The source is of course datablock which contains the data. And we're copying 20 characters. This is goody boody, but now how do we get last_name out of that sucker!?

Easy as pie (mmmmmmmm, pie-goodness)! Its basically the same statement structure, we just plug in what we know about the last name field: 1.) its 20 characters long, 2.) its required on most birth certificates, 3.) the first character of the data for it starts at subscript 20 in datablock, and 4.) it will be stored in the last_name variable. So putting what we may or may not know into motion:
memcpy(last_name, datablock + 20, 20);
Pretty cool huh? In English this is telling the computer to get 20 characters from datablock starting at the 20th position (which is where our field info begins). The destination for this information is of course last_name. That was pretty simple, you'll get the hang of it pretty quick.

And NOW the hardest part! I'm being sarcastic actually. But in truth after using a ton of memcpy()'s and other string/array functions people being to lose track of the basics. By this point you may actually be thinking that you're going to need to use a function to get the middle initial field from datablock. But stop and think back to the basics!

What do we know about this field? Its one byte long. It resides in subscript 40 of datablock. Its destination variable is middle_inital. Well, gee! This is more than pie easy (mmmmmm, more pie *smacking lips*). This is what we do:
middle_initial = datablock[40];
; Pretty darn difficult, huh? Remember that an array is simply made up of several individual pieces. In this case our array was made up of several character variables, forming a C-style "string". Our one variable was a single character and therefore we could store the value from one piece of our array into the one variable with an orthodox equal sign.

Some of you may have tried using a function and it didn't work. So can you use a memcpy() in this situation? Of course. Its a little different and I will NOT explain it here and now, but you can take a peak:
memcpy(&middle_initial, datablock + 40, 1);
Pretty cool, huh? Yes, step away from me slowly, I am a crazy techno-geek and I just might bite (if you're lucky). And now for an example program which does what you've seen above, but then puts the pieces back together. You're going to have to disect the latter because I'm tired and going to bed .
/* -example-source------------------------------[ string breakdown ]- */

// need my headers dammit
#include <iostream.h>
#include <string.h>

void main()
{
  // declare variables
  char datablock[81];
  char first_name[21];
  char last_name[21];
  char middle_initial;

  // --------- clear variables to some default values

  // first name
    memset (first_name, ' ', 20);
    first_name[20] = 0;

  // last name
    memset (last_name, ' ', 20);
    last_name[20] = 0;

  // middle initial
    middle_initial = ' ';

  // our nifty data block
    memset (datablock+41, ' ', 39);
    memcpy (datablock, "Leo                 LionHeart           C", 41);
    datablock[80] = 0;

  // --------- output the initial stuff to user
  
  cout << "String Breakdown - *yee haw!*" << endl << endl
       << "first_name = \"" << first_name << "\"" << endl
       << "last_name = \"" << last_name << "\"" << endl
       << "middle_initial = \"" << middle_initial << "\"" << endl
       << "datablock = \"" << datablock << "\"" << endl << endl
       << "Now extracting individual field values ..." << endl;

  // --------- extract individual field values
  memcpy (first_name, datablock + 0, 20);
  memcpy (last_name, datablock + 20, 20);
  middle_initial = datablock[40];

  // --------- show the user
  cout << "first_name = \"" << first_name << "\"" << endl
       << "last_name = \"" << last_name << "\"" << endl
       << "middle_initial = \"" << middle_initial << "\"" << endl
       << "datablock = \"" << datablock << "\"" << endl << endl
       << "Now changing individual field variables ..." << endl;

  // --------- change the individual field variables
  memcpy (first_name, "Pig", 3);
  memcpy (last_name, "SatinHooves", 11);
  middle_initial = 'D';

  // --------- show the user
  cout << "first_name = \"" << first_name << "\"" << endl
       << "last_name = \"" << last_name << "\"" << endl
       << "middle_initial = \"" << middle_initial << "\"" << endl
       << "datablock = \"" << datablock << "\"" << endl << endl
       << "Now putting new values into 'datablock' variable ..." << endl;

  // --------- stick fields into 'datablock' variable
  memcpy (datablock + 0, first_name, 20);
  memcpy (datablock + 20, last_name, 20);
  datablock[40] = middle_initial;

  // --------- show the user
  cout << "first_name = \"" << first_name << "\"" << endl
       << "last_name = \"" << last_name << "\"" << endl
       << "middle_initial = \"" << middle_initial << "\"" << endl
       << "datablock = \"" << datablock << "\"" << endl << endl
       << "THE END!" << endl;
}

/* ------------------------------------------------------------------ */
Sorting
Multi-Dimensional Arrays
Relationship of Pointers and Arrays

The End (for now, *muh ha ha ha*)

Page Content & Design ©1998
By Neil C. Obremski

home

prfce

loop

refs

ptrs

struct

class

array

rsrc

site

indx