| | |
Need Help with an array problem
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2005
Posts: 22
Reputation:
Solved Threads: 0
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(); }
C Syntax (Toggle Plain Text)
strcpy(set1[i].status,datalist[random_number]);
strcpy(set1[i].status,&datalist[random_number][20]);
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
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]);
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
![]() |
Similar Threads
- Array problem (Java)
- Array problem (C++)
- Array problem (C#)
- Is there a simplest way to work this array problem? (C++)
- class array problem! (C++)
- C++ help with student array problem (C++)
- i have problem with array plz help (Java)
- Large Array Problem (PHP)
Other Threads in the C Forum
- Previous Thread: Using a variable as an array's index?
- Next Thread: Factorial function ?
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks bash binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile csyntax directory dynamic fflush file fork frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling scripting segmentationfault send shape socketprograming spoonfeeding stack standard string strings structures suggestions systemcall test testautomation unix user variable voidmain() wab win32api windows.h






