Array of strings
mmonnin
Centreville, VA
Ok I cant think of how to do this. I want an array of some type of data type, not necessarily a string data type, that will hold a several characters like a string.
I dont want a char array. Each element in the array needs to hold up to about 8 characters, alpha characters to be exact.
I am going to have a random generator generate a numer from 0 to 3 and depending on that number display the corresponding element in the array in a printf statement.
I am pretty sure I have done this in the past but I am having a brain fart at the moment and google isnt returning what I want, just an array of chars or double arrays.
The lab I am working on is interesting and deals with several threads in windows. All the Critical Section stuff with shared variables, man! I didnt realize all the stuff OSs had to deal with.
Any help would be appreciated, I am sure its pretty easy but I am just out of it right now.
I dont want a char array. Each element in the array needs to hold up to about 8 characters, alpha characters to be exact.
I am going to have a random generator generate a numer from 0 to 3 and depending on that number display the corresponding element in the array in a printf statement.
I am pretty sure I have done this in the past but I am having a brain fart at the moment and google isnt returning what I want, just an array of chars or double arrays.
The lab I am working on is interesting and deals with several threads in windows. All the Critical Section stuff with shared variables, man! I didnt realize all the stuff OSs had to deal with.
Any help would be appreciated, I am sure its pretty easy but I am just out of it right now.
0
Comments
Oh and for anyone else using threads in a program, cout is not thread safe, so thats the reason for printf instead of cout.
I would still like to know how to do that that. Like use 'string varName[4]' as an array of 4 strings or something.
char [number][length]
where number is the number of strings in the array, and length is the length of each string in that array. you can then access an element in the array by using only the left index, i.e. array[5] to access the 6th element of the array. if the number of strings or length of each string is dynamic and you don't want to use a fixed buffer length, you can use pointers and the memory allocation functions.
How can I dispay that in a printf statement. That would require calling the char variable as many times as there are letters. And the words are not the same length.
Never heard of calloc or malloc either. Hmm....they didnt need to be created while the program was running. They were going to set before hand.
I ended up not needed it anyway, but I learned something else.
also, you would use malloc() if you made an array of character pointers. you could still access those elements individually if you wanted as well. you'd use the method TheBaron suggested if you didn't know the length of the strings or number of elements. Both can be allocated on-the-fly using a pointer to a pointer.
will print
The above code is shows that you don't need to reference each character in a string. you reference the array element itself which returns the entire string (until it hits a null byte).
If you wanted to display everything in the array without referencing every element in the array, use a loop to combine all the individual elements into a single c-string and then use printf().