My C is a little more than rusty at the moment, so I\'m failing to create something I think should be pretty basic.
Allow me to refer to character arrays as strings
This example was done with turbo c+ 1.01 dos and works in the 3.0 dos version also.
char * text[] = {
"message1",
"message2",
"message3"
};
Note that your examples show arrays of pointers. If you want arrays of arrays (multidimensional arrays) specify all the sizes in the array definition.
char sentences[500][42]; /* sentences is an array of 500 elements.
** each element is itself an array
** capable of holding strings up to length 41 */
you should learn what an array means. an array is basically a set of integer or character or anything. when you are storing a character value in an array, define it as,
char array[] = {"somestringhere"};
now you want to store such arrays in an another array. it is simple:
char* array1[];
the array1 will store values, which are of char*
type i.e. the address of character arrays.
now you want to store these in other array,
char** array2[];
this is, array of [address of arrays] now, all you have to do is;
array1[0] = array; //same as: array1[0] = *array[0];
array2[0] = *array1[0];
Now you've everything you need. Hope you are clear, to the core. :)
You have one-too-many pointers in both of your arrays.
char arrayOfChars[50]; // a single array of characters
char *arrayOfArraysOfChars[10]; // array to hold multiple single arrays of characters
Since the arrayOfChars is being used like a buffer (new data always goes there first), you'll need to save a copy of the string into the arrayOfArrays. The POSIX function strdup
should help here.
Notice &
and *
are opposites, so &*
and *&
do absolutely nothing.
You could also, make the arrayOfArrays literally that.
char arrayOfChars[50]; // a single array of characters
char arrayOfArraysOfChars[10][50]; // array to hold multiple single arrays of characters
With this setup, you should use strcpy
to copy the data into arrayOfArrays.
Having read your edit, I think you need to start real simple. And FWIW the variable names are the wrong kind of Hungarian.
For what I think you're trying to do, I'd start with just a single char array. This will be the main buffer, to hold strings that are being input and examined.
enum { BUFSZ = 50 };
char buf[BUFSZ + 1];
Then you can use it with fgets
or whatever.
fgets(buf, BUFSZ, infile);
To save these up in an array, I'd use strdup
for its automatic trimming. If the strings are going to be mostly 2 characters long, I don't want 48 extra bytes being used for each one. So, an array of char pointers (strings).
enum { STRVSZ = 40 };
char *strv[STRVSZ + 1];
int i;
i = 0;
strv[i] = strdup(buf);
strv[i+1] = NULL; // This makes it an "argv-style" NULL-terminated array of strings
++i; // i is now the index of the next element, and a count of elements already added
Each element of strv
is a char pointer. But to preserve our sanity, we're trying to abstract away some of that distracting detail for a moment, and treat strings as a separate data type.
Now to create lists of these, we do the same thing again. But there's no strdup
-type function to make a duplicate of an array of pointers, so we have to separate the allocation and copying.
enum { STRVVSZ = 20 };
char **strvv[STRVVSZ + 1];
int j;
j = 0;
strvv[j] = calloc(i+1, sizeof *strvv[j]); // assuming i is the count of elements
memcpy(strvv[j], strv, i * sizeof *strvv[j]);
++j; // j is now the index of the next string-pointer array in the array-of-same,
// and a count of elements already added.
Now, my names are just as silly as yours, but they're shorter!
if storing text in a .c or .h file having more than one line of text, equivalent to the idea of an array of char arrays. can do this:
char * text[] = {
"message1",
"message2",
"message3"
};
can also use char *text[], char near *text[], char far *text[], char huge *text[]. has to have an asterisk or star character for a pointer.
a for loop can be used to display text:
char i; // int type can also be used
for (i = 0, i < 3; i++)
printf("%s\n", text[i]);
other loops:
char i = 0; // may not be zero when declared as "char i;" only
while (i < 3) {
printf("%s\n", text[i]);
i++;
}
or
char i = 0; // may not be zero when declared as "char i;" only
do {
printf("%s\n", text[i]);
i++;
} while (i < 3);