I have a function in one of my programs that picks a random state from a list. It then puts this random state in a structure array. However the name selected from the list does not match the name that winds up in my array structure. The following is not snipped from my code, since I'm using multifiles, so I just retyped it all in one program so it could be copied and pasted to your compiler if you wish to try it. The results of this program are the same as mine though. Basically, I'd expect the output of the last two printf() functions to come out the same for each loop.

// data list of states
[B]char[/B] datalist[10][20]=

// structures
[B]struct[/B] data_set
   [B]char[/B] status[20];
   [B]int[/B] priority;
   [B]int[/B] complexity;

[B]int[/B] main()
     // declarations
     [B]struct[/B] data_set set1[5];
     [B]int[/B] random_number;
     // implementation
     srand(time(NULL));  // for random picking from data list
     [B]for[/B]([B]int[/B] i=0;i<5;i++)
          // get a random state from a list of 10          
          printf("Random Number:                %d\n",random_number);
          printf("Random Name picked from list: %s\n",datalist[random_number]);
          printf("Random Name stored in set1:   %s\n",set1[i].status);
13 Years
Discussion Span
Last Post by Auto

Thanks, that fixed it. I was just under the impression that since it was an array of strings that I needed the address operator in the strcpy() function, and the only way I could get that to work was add the 20.

Edited by happygeek: fixed formatting


You can do that if you want to, but don't go off the end of the array.


Now I think I get it, I was basically offsetting the location I was pointing to by 20. Here I was assuming I needed to put the length of each string in the brackets, because I think empty brackets caused an error. I Figured it needed to know how big of a string it was copying.

Thanks again Dave.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.