quyvle said:
Could someone show me how to get this accomplished? I'm somewhat
confused. I thought you were suppose to make an array of pointers and
those pointers point to strings of whatever length.
Definitely *no*. An array of pointers is an array of pointer,
nothing more. Once you have defined an array of pointers the
pointers don't point to anything you would be able to use,
they pont to some random places in memory. Before you can use
them in any way _you_ have to make them point to memory you own
(obtained by either defining a variable or array or calling a
function like malloc()). And, moreover, there are no strings of
"whatever" length in C. If you want a string of a _certain_
length you must ask for it, either by defining an array of chars
or by calling malloc() (if you use malloc() and you're lucky,
the system will give it to you). Once you got it you can use
its exact length, but not a single character more - if you get
it wrong you're in for bad surprises: it could work anyway (but
suddenly fail if you update your compiler or tray it on a different
system), the program could crash in some unforseeable place or it
could look like it works but the results of the program are wrong.
If you come from a Perl background then you have to get rid of
quite a number of assumptions. Perl (and other higher-level
languages) frees you from the burden of thinking very carefully
about where memory comes from - if you have a variable, memory is
suddenly there when you need it. Moreover, if you have an array,
then you can put all kinds of things into it - one element could
be an integer, the next one a string, followed by a hash etc.,
but that's also something you won't get in C. To do this Perl
does a lot of "magic" in the background (that's one reason why
Perl scripts tend to be slower than C programs). But in C nothing
of this is happening, you must do it all by yourself. And you must
be careful to get it right, otherwise all bets are off.
If you only need an array of NUM_STRINGS strings of all the same
length STRING_LENGTH, then something like
char my_string[ NUM_STRINGS ][ STRING_LENGTH ];
is the definition you need. But if you e.g. don't know how long
the different strings are going to be then you would first need
an array of char pointers
char *my_string[ NUM_STRINGS ];
and, once you know how long the individual strings are going to be,
call malloc() for each of the elements of this array to obtain
enough memory. I.e. if you know that the first of the strings is
going to need 99 chars you would call
if ( ( my_string[ 0 ] = malloc( 100 ) ) == NULL ) {
fprintf( stderr, "malloc() failure\n" );
exit( EXIT_FAILURE );
}
If everything goes well you end up with the first pointer of the
array pointing to enough memory for a string consisting of 99
characters (the extra one is going to be needed for the trailing
'\0' character that indicates the end of the string). Since you
can never be sure that everything goes well, you have to check
every time you request memory.
[Hint: never cast the return value of malloc(). If you do all
you get out of it is keeping the compiler from warning you if
you forgot to include <stdlib.h> and, if you did it, creating
a bug that can be extremely hard to find since then the return
value of malloc() is automatically converted to an int before
the resulting integer value is re-converted via the cast to a
pointer, which unfortunately works without a problem on a set
of (popular) machines, but fails badly on others.]
If you don't know how long the string you will have to read is
going to be you're in for even some more work. You have to come
up with a method to determine what's going to be needed, then
allocate memory and only then get hold of the string and put it
where you want it. That's why a function like fscanf() is rather
difficult to use with string input (and also other user input!) -
you hardly ever know an upper bound on the length of the string.
Thus you typically resort to using fgets() where you can read in
a certain number of characters, test if you got no more than fits
into the memory you have (in that case you're done) or the memory
got filled up but there's more, so you need to allocate more memory
and continue to read.
All these things are done in Perl automagically, so you don't got
to worry about it. But something in the innards of Perl is exactly
doing it for you (Perl is implemented in C) - and if you write in
C you have to take care of it all by yourself. All that may look
tedious (it often is

or even scary, but once you get the hang
of it it's not _that_ complicated.
Regards, Jens