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

array of strings

P: 9
Hi guys,

i m have trouble with reading strrings and storing them in a 2d array and printing them

(1)
Expand|Select|Wrap|Line Numbers
  1. for(y=0;y<5;y++)
  2.     {
  3.      printf("enter filenames \n");
  4.      scanf("%s",&fnames[y][0]);
  5.     }
  6.  
  7.   for(y=0;y<5;y++)
  8.     {
  9.      printf("filename %s\n",&fnames[y][0]);
  10.     }
  11.  
  12. (2) for(y=0;y<5;y++)
  13.     {
  14.      printf("enter filenames \n");
  15.      scanf("%s",fnames[y]);
  16.     }
  17.  
  18.   for(y=0;y<5;y++)
  19.     {
  20.      printf("filename %s\n",fnames[y]);
  21.     }
  22.  
for both the programs if the input were to be:
aaa
bbb
ccc
ddd
eee

the output is
aaabbb......eee
bbb.....eee
cc...eee
d...eee
eee

what could be wrong?
i thought fname[y] refers to the base address of yth 1D array in the 2D array.
Mar 24 '08 #1
Share this Question
Share on Google+
6 Replies


P: 1
Hi guys,

i m have trouble with reading strrings and storing them in a 2d array and printing them

(1)for(y=0;y<5;y++)
{
printf("enter filenames \n");
scanf("%s",&fnames[y][0]);
}

for(y=0;y<5;y++)
{
printf("filename %s\n",&fnames[y][0]);
}

(2) for(y=0;y<5;y++)
{
printf("enter filenames \n");
scanf("%s",fnames[y]);
}

for(y=0;y<5;y++)
{
printf("filename %s\n",fnames[y]);
}

for both the programs if the input were to be:
aaa
bbb
ccc
ddd
eee

the output is
aaabbb......eee
bbb.....eee
cc...eee
d...eee
eee

what could be wrong?
i thought fname[y] refers to the base address of yth 1D array in the 2D array.




i dnt think anything wrong its workin fine in vc++ compiler
Mar 24 '08 #2

P: 9
i used gcc in fedora linux..cant figure out the error..
Mar 24 '08 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
How is your array defined?
Mar 24 '08 #4

P: 9
hey i got the error..eventhough the program is correct i overlooked some basics..

my array defn was char fnames[5][3]..//i thought this can 3 character strings,but that is not the case..the compiler appends a zero after each input..

so char fnames[5][4] should be correct..
how is your array defined
weaknessforcats ..u were bang on target!!

basics!!
Mar 24 '08 #5

Natasha26
P: 12
Just a quick check. Does the following sound ok to anyone? It works, but i don't know whether the array gets de-allocated after main() ends?
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. int main()
  4. {
        char* fname[3];
  5.  
  6.     fname[0] = "aaa";
  7.     fname[1] = "bbb";
  8.     fname[2] = "ccc";
  9.  
  10.     for(int i=0; i<3; i++)
  11.         std::cout << "\n" << fname[i];
  12.  
  13.    // The following should be superflous right? 
  14.    /* for(int i=0; i<3; i++)
  15.          delete [] fname[i];
  16.    */
  17.  
  18.   return 0;}
  19.  
Mar 24 '08 #6

weaknessforcats
Expert Mod 5K+
P: 9,197
What ytou have defined is an array of 3 char*. This is OK.
Next, each char* is assigned the address of a const string. This is OK.
Next, you display. This is OK.
Next, you delete. This is a crash. You do not delete unless you used the new operator to allocate. When you don't use new, the compiler will take care of deleting your variables.
Mar 25 '08 #7

Post your reply

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