By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

string arrays

P: 3
hey, i have only been working with c for a very short time and i have a queston that seems to be poorly covered in all areas i have looked.

i am attempting to create an array of strings so that 9 strings start out as "----------" then are replaced by user input with a for loop. Knowing that strings are just character arrays, i initially tried just creating a 2d character array but it became quickly apparent that this was not the best way to go about it. after some research, i have been trying to use pointers, but the code is still not working properly. relevant code is as follows:

Expand|Select|Wrap|Line Numbers
  1. char *batter[9] = {
  2.     "----------",
  3.     "----------",
  4.     "----------",
  5.     "----------",
  6.     "----------",
  7.     "----------",
  8.     "----------",
  9.     "----------",
  10.     "----------"
  11. };
  12. int main(int argc,char *argv[])
  13. {
  14.      for(l = 0; l < 9; l++) {
  15.           printf("\n\nWhat is the name of batter %d?",l+1);
  16.           gets(batter[l]);
  17.           clr();
  18.           for(k = 0; k < 9; k++) {
  19.     printf("%s \n",batter[k]);
  20.           }
  21.      }
  22.      return 0;
  23. }
unfortunetly, this every time the user enters a string, it changes all strings to that value instead of only the intended one. a steer in the correct direction would be greatly appreaciated.
Thanks.
Mar 8 '07 #1
Share this Question
Share on Google+
4 Replies


Ganon11
Expert 2.5K+
P: 3,652
Instead of using nested for...loops, you should use 2 separate loops - the first should fill the array (ending after the gets call), and the second should display (starting after the clr() call).
Mar 8 '07 #2

P: 3
that's not really what i'm trying to do, if possible i'd like to print them all after every entry.

regardless, i tried it anyway and it still just prints them all as the last entry. and other suggestions on how to make it work either way would be great.
Mar 9 '07 #3

Expert 100+
P: 1,510
in this definition
Expand|Select|Wrap|Line Numbers
  1. char *batter[9] = {
  2.     "----------",
  3.     "----------",
  4.     "----------",
  5.     "----------",
  6.     "----------",
  7.     "----------",
  8.     "----------",
  9.     "----------",
  10.     "----------"
  11. };
  12.  
the strings "----------" are constants and attempting to change them will give a segmentation error.
Allocate a 2D array initialised with the strings, e.g.
Expand|Select|Wrap|Line Numbers
  1. char batter[9][12] = {
  2.     "----------",
  3.     "----------",
  4.     "----------",
  5.     "----------",
  6.     "----------",
  7.     "----------",
  8.     "----------",
  9.     "----------",
  10.     "----------"
  11. };
  12. int main(int argc,char *argv[])
  13. {
  14.      int l,k;                             // added
  15.      for(l = 0; l < 9; l++) {
  16.           printf("\n\nWhat is the name of batter %d?",l+1);
  17.           gets(batter[l]);
  18.           //clr();
  19.           for(k = 0; k < 9; k++) {
  20.     printf("%s \n",batter[k]);
  21.           }
  22.      }
  23.      return 0;
  24. }
  25.  
  26.  
you should check that the string read by fgets() does not exceed the storage in the array
Mar 9 '07 #4

P: 3
thank you, that did it, appreaciate the all the help
Mar 9 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.