array of strings

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sritejv
    New Member
    • Mar 2008
    • 9

    array of strings

    Hi guys,

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

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

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

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

    for(y=0;y<5;y++ )
    {
    printf("filenam e %s\n",fnames[y]);
    }
    [/CODE]
    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.
    Last edited by sicarie; Mar 24 '08, 04:00 AM. Reason: code tags
  • bharatkukreti
    New Member
    • Mar 2008
    • 1

    #2
    Originally posted by sritejv
    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",&fna mes[y][0]);
    }

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

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

    for(y=0;y<5;y++ )
    {
    printf("filenam e %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

    Comment

    • sritejv
      New Member
      • Mar 2008
      • 9

      #3
      i used gcc in fedora linux..cant figure out the error..

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        How is your array defined?

        Comment

        • sritejv
          New Member
          • Mar 2008
          • 9

          #5
          hey i got the error..eventhou gh 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!!

          Comment

          • Natasha26
            New Member
            • Mar 2008
            • 12

            #6
            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?
            Code:
            #include <iostream>
            
            int main()
            {[INDENT]    char* fname[3];
                
                fname[0] = "aaa";
                fname[1] = "bbb";
                fname[2] = "ccc";
                
                for(int i=0; i<3; i++)
                    std::cout << "\n" << fname[i];
            
               // The following should be superflous right? 
               /* for(int i=0; i<3; i++)
                     delete [] fname[i];
               */
            
              return 0;[/INDENT]
            }

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              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.

              Comment

              Working...