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...".first_name
, last_name
, middle_initial
.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.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!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!?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.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!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.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;
}
/* ------------------------------------------------------------------ */
Page Content & Design ©1998 By Neil C. Obremski |
home |
prfce |
loop |
refs |
ptrs |
struct |
class |
array |
rsrc |
site |
indx |