943,936 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1794
  • C RSS
Mar 30th, 2005
0

Need Help with an array problem

Expand Post »
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.

#include<time.h>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>  
// data list of states
char datalist[10][20]=
{
      "complete",
      "considering",
      "active",
      "disabled",
      "obsolete",
      "halted",
      "expanded",
      "rerouted",
      "initialized",
      "reworked"
};

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

int main()
{
     // declarations
     struct data_set set1[5];
     int random_number;
     
     // implementation
     srand(time(NULL));  // for random picking from data list
     for(int i=0;i<5;i++)
     {
          // get a random state from a list of 10          
          random_number=rand()%10;
          printf("Random Number:                %d\n",random_number);
          printf("Random Name picked from list: %s\n",datalist[random_number]);
          strcpy(set1[i].status,&datalist[random_number][20]);
          printf("Random Name stored in set1:   %s\n",set1[i].status);
     }
     getch();
}
Similar Threads
Reputation Points: 11
Solved Threads: 1
Light Poster
Auto is offline Offline
33 posts
since Mar 2005
Mar 30th, 2005
0

Re: Need Help with an array problem

  1. strcpy(set1[i].status,datalist[random_number]);
not
strcpy(set1[i].status,&datalist[random_number][20]);
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Mar 30th, 2005
0

Re: Need Help with an array problem

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].
Reputation Points: 11
Solved Threads: 1
Light Poster
Auto is offline Offline
33 posts
since Mar 2005
Mar 30th, 2005
0

Re: Need Help with an array problem

You can do that if you want to, but don't go off the end of the array.
strcpy(set1[i].status,&datalist[random_number][0]);
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Mar 30th, 2005
0

Re: Need Help with an array problem

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.
Reputation Points: 11
Solved Threads: 1
Light Poster
Auto is offline Offline
33 posts
since Mar 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Using a variable as an array's index?
Next Thread in C Forum Timeline: Factorial function ?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC