Storing 2D char array
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Storing 2D char array

  1. #1
    Join Date
    Nov 2012
    Posts
    6

    Storing 2D char array

    Hi, I am unable to understand why is the output for this shows me the address of the stored array.

    int main()
    {
    size_t k= 0 ;
    char t[3][100] = { "hi", "di", "fi" } ;
    char s[3][100] ;
    for (int r = 0 ; r < 3 ; r++)
    {
    size_t k = strlen (t[r]);
    for ( int c = 0 ; c < k ; c++)
    {


    s[r][c] = t[r][c] ;
    // cout << s[r][c] ; // this works perfect to show me the output
    }
    }

    cout << s[r][c] ;

    }

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,232

    Re: Storing 2D char array

    You need to learn to use code tags.
    You need to learn to use your debugger.
    What is the value of r and c in your last cout statement?

  3. #3
    Join Date
    Apr 1999
    Posts
    27,446

    Re: Storing 2D char array

    Quote Originally Posted by naildirt View Post
    Hi, I am unable to understand why is the output
    How can you get output from a program that cannot be compiled successfully? What compiler are you using (version and number)?

    First, even if you add the missing headers, the code should not have compiled:
    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int main()
    {
        size_t k= 0 ;
        char t[3][100] = { "hi", "di", "fi" } ;
        char s[3][100] ;
        for (int r = 0 ; r < 3 ; r++)
        { 
           size_t k = strlen (t[r]); 
           for ( int c = 0 ; c < k ; c++)
           {
              s[r][c] = t[r][c] ;
           }
        }
        cout << s[r][c] ; // <--- How did this line compile??
    }
    This is not valid ANSI C++ code. The variables "r" and "c" are not available outside of where they were declared as loop counters. So how could you have created a program with incorrect C++ syntax?
    for this shows me the address of the stored array.
    For me, it shows nothing since no program was even created due to syntax errors.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Nov 2012
    Location
    Kolkata, WestBengal, India
    Posts
    15

    Re: Storing 2D char array

    Yes, exactly. The code will not get compiled.

    Now, if you define 'r' and 'c' outside the first for loop, at this point it shouldn't show any specific values -
    cout << s[r][c] ;
    coz, at this point it will be out of the range of the array s - here the line will get resolved to -
    cout << s[3][2] ; <== whcih yet not get assigned

    Hope, my points are clear here...

    Raju2001006

    Please put [CODE][/CODE] tags around your code to preserve indentation and make it more readable...

  5. #5
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,612

    Re: Storing 2D char array

    Quote Originally Posted by raju2001006 View Post
    ... here the line will get resolved to -
    cout << s[3][2] ; <== whcih yet not get assigned

    Hope, my points are clear here...
    I have no idea why you need to output the value of some array element outside the loop, however, using 3 as the first index of array s is illegal: only indexes 0 to 2 are valid since this array has only 3 rows.
    Victor Nijegorodov

  6. #6
    Join Date
    Nov 2012
    Location
    Kolkata, WestBengal, India
    Posts
    15

    Re: Storing 2D char array

    Technically speaking, s[3][2] is not illegal - it's just not good practice to use such invalid array counters.

    You can actually assign something in s[3][2]; - but the Stack around s may get corrupted because of this.

    use something like -
    s[3][0]='R'
    cout << s[3][0]; <<== you will surely get the value stored at s[3][0].

    Raju2001006

    Please put [CODE][/CODE] tags around your code to preserve indentation and make it more readable...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center