Array of strings

mmonninmmonnin Centreville, VA
edited August 2005 in Internet & Media
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.

Comments

  • mmonninmmonnin Centreville, VA
    edited August 2005
    OK well I am not sure if I am going to use it in a printf statement but rather a case statment comparison and inside the case statment display a certain printf statement.

    Oh and for anyone else using threads in a program, cout is not thread safe, so thats the reason for printf instead of cout.
  • mmonninmmonnin Centreville, VA
    edited August 2005
    Bah, screw it. I am going to use a case statement after the number gen to do it all since several opertions will depend on that random variable.

    I would still like to know how to do that that. Like use 'string varName[4]' as an array of 4 strings or something.
  • TheBaronTheBaron Austin, TX
    edited August 2005
    define a char** variable and use calloc or malloc to dynamically define its size
  • NosferatuNosferatu Arizona
    edited August 2005
    you could use:

    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.
  • mmonninmmonnin Centreville, VA
    edited August 2005
    I dont want a char array.

    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.
  • NosferatuNosferatu Arizona
    edited August 2005
    It wouldn't require calling the variable as many times as there are letters. It's not a char variable, it's an array of c-strings (two-dimensional array). you can reference an element by using the left index.

    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.
    #include < stdio.h >
    
    int main() {
            char test[3][10];
    
            strcpy(test[0],"This");
            strcpy(test[1],"is a");
            strcpy(test[2],"test!");
    
            printf("%s %s %s\n",test[0],test[1],test[2]);
            return 0;
    }
    

    will print
    This is a test!

    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().
  • mmonninmmonnin Centreville, VA
    edited August 2005
    Hmm I have never used multidimentional arrays like that. I guess the functionality is built into the strcpy function?
  • NosferatuNosferatu Arizona
    edited August 2005
    Well, strcpy() is still just receiving a c-string (test[#]), it's the same (as far as strcpy() is concerned) if you declared a character array (c-string) and then copied "a string" into it.
  • TheBaronTheBaron Austin, TX
    edited August 2005
    in fact, rather than uysing strcpy, all you really have to do is null terminate the string -- that is, add a 0 element after the last character, and then printf with %s should work just fine
Sign In or Register to comment.