-
November 20th, 2012, 11:19 AM
#1
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] ;
}
-
November 20th, 2012, 03:54 PM
#2
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?
-
November 20th, 2012, 04:16 PM
#3
Re: Storing 2D char array
Originally Posted by naildirt
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
-
November 21st, 2012, 07:59 AM
#4
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...
-
November 21st, 2012, 08:58 AM
#5
Re: Storing 2D char array
Originally Posted by raju2001006
... 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
-
November 22nd, 2012, 04:46 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|